Merge "Fix flaky IpMemoryStoreServiceTest.testFullMaintenance/testInterruptMaintenance"
diff --git a/Android.bp b/Android.bp
index f17412d..27a1044 100644
--- a/Android.bp
+++ b/Android.bp
@@ -19,20 +19,26 @@
 // (InProcessNetworkStack). The following structure is used to create the build rules:
 //
 //                          NetworkStackAndroidLibraryDefaults <-- common defaults for android libs
-//                                     /           \
-//    +NetworkStackApiStableShims --> /             \ <-- +NetworkStackApiCurrentShims
-//    +NetworkStackApiStableLevel    /               \    +NetworkStackApiCurrentLevel
-//                                  /                 \
-//           NetworkStackApiStableLib         NetworkStackApiCurrentLib <-- android libs w/ all code
-//                     |                                     |             (also used in unit tests)
-//                     | <--   +NetworkStackAppDefaults  --> |
-//                     |          (APK build params)         |
-//                     |                                     |
-//                     | <-- +NetworkStackApiStableLevel     | <-- +NetworkStackApiCurrentLevel
-//                     |                                     |
-//                     |                                     |
-//           NetworkStackApiStable          NetworkStack, InProcessNetworkStack, <-- output APKs
-//                                                    TestNetworkStack
+//                                            /    \
+//           +NetworkStackApiStableShims --> /      \ <-- +NetworkStackApiCurrentShims
+//           +NetworkStackApiStableLevel    /        \    +NetworkStackApiCurrentLevel
+//           +jarjar apistub.api[latest].* /          \   +module src/
+//            to apistub.*                /            \
+//                                       /              \
+//         NetworkStackApiStableDependencies             \
+//                                     /                  \               android libs w/ all code
+//                   +module src/ --> /                    \              (also used in unit tests)
+//                                   /                      \                        |
+//               NetworkStackApiStableLib               NetworkStackApiCurrentLib <--*
+//                          |                                     |
+//                          | <--   +NetworkStackAppDefaults  --> |
+//                          |          (APK build params)         |
+//                          |                                     |
+//                          | <-- +NetworkStackApiStableLevel     | <-- +NetworkStackApiCurrentLevel
+//                          |                                     |
+//                          |                                     |
+//                NetworkStackApiStable          NetworkStack, InProcessNetworkStack, <-- APKs
+//                                                         TestNetworkStack
 
 // Common defaults to define SDK level
 java_defaults {
@@ -43,22 +49,29 @@
 
 java_defaults {
     name: "NetworkStackApiStableLevel",
-    sdk_version: "system_current", // TODO: change to system_29
+    sdk_version: "system_29",
     min_sdk_version: "28",
 }
 
-// Java libraries for the API shims
+// Filegroups for the API shims
 filegroup {
     name: "NetworkStackApiCurrentShims",
     srcs: [
-        "apishim/current/**/*.java"
+        "apishim/common/**/*.java",
+        "apishim/29/**/*.java",
+        "apishim/current/**/*.java",
+        ":net-module-utils-srcs",
     ],
 }
 
+// API stable shims only include the compat package, but it is jarjared to replace the non-compat
+// package
 filegroup {
     name: "NetworkStackApiStableShims",
     srcs: [
-        "apishim/29/**/*.java"
+        "apishim/common/**/*.java",
+        "apishim/29/**/*.java",
+        ":net-module-utils-srcs",
     ],
 }
 
@@ -67,7 +80,6 @@
 java_defaults {
     name: "NetworkStackAndroidLibraryDefaults",
     srcs: [
-        "src/**/*.java",
         ":framework-networkstack-shared-srcs",
         ":services-networkstack-shared-srcs",
         ":statslog-networkstack-java-gen",
@@ -80,25 +92,38 @@
         "networkstackprotosnano",
         "captiveportal-lib",
     ],
-    manifest: "AndroidManifestBase.xml",
     plugins: ["java_api_finder"],
 }
 
 // The versions of the android library containing network stack code compiled for each SDK variant
+// API current uses the sources of the API current shims directly.
+// This allows API current code to be treated identically to code in src/ (it will be moved
+// there eventually), and to use the compat shim as fallback on older devices.
 android_library {
     name: "NetworkStackApiCurrentLib",
     defaults: ["NetworkStackApiCurrentLevel", "NetworkStackAndroidLibraryDefaults"],
-    srcs: [
-        ":NetworkStackApiCurrentShims",
-    ],
+    srcs: [":NetworkStackApiCurrentShims", "src/**/*.java"],
+    manifest: "AndroidManifestBase.xml",
+}
+
+// For API stable, first build the dependencies using jarjar compat rules, then build the sources
+// linking with the dependencies.
+java_library {
+    name: "NetworkStackApiStableDependencies",
+    defaults: ["NetworkStackApiStableLevel", "NetworkStackAndroidLibraryDefaults"],
+    srcs: [":NetworkStackApiStableShims"],
+    jarjar_rules: "apishim/jarjar-rules-compat.txt",
 }
 
 android_library {
     name: "NetworkStackApiStableLib",
-    defaults: ["NetworkStackApiStableLevel", "NetworkStackAndroidLibraryDefaults"],
-    srcs: [
-        ":NetworkStackApiStableShims",
+    defaults: ["NetworkStackApiStableLevel"],
+    srcs: [":framework-annotations", "src/**/*.java"],
+    // API stable uses a jarjared version of the shims
+    static_libs: [
+        "NetworkStackApiStableDependencies",
     ],
+    manifest: "AndroidManifestBase.xml",
 }
 
 // Common defaults for compiling the actual APK, based on the NetworkStackApiXBase android libraries
diff --git a/apishim/29/com/android/networkstack/apishim/SocketUtilsShimImpl.java b/apishim/29/com/android/networkstack/apishim/api29/SocketUtilsShimImpl.java
similarity index 70%
rename from apishim/29/com/android/networkstack/apishim/SocketUtilsShimImpl.java
rename to apishim/29/com/android/networkstack/apishim/api29/SocketUtilsShimImpl.java
index 0e41e19..be12662 100644
--- a/apishim/29/com/android/networkstack/apishim/SocketUtilsShimImpl.java
+++ b/apishim/29/com/android/networkstack/apishim/api29/SocketUtilsShimImpl.java
@@ -14,18 +14,32 @@
  * limitations under the License.
  */
 
-package com.android.networkstack.apishim;
+package com.android.networkstack.apishim.api29;
 
 import android.net.util.SocketUtils;
 
 import androidx.annotation.NonNull;
 
+import com.android.networkstack.apishim.SocketUtilsShim;
+
 import java.net.SocketAddress;
 
 /**
  * Implementation of SocketUtilsShim for API 29.
  */
 public class SocketUtilsShimImpl implements SocketUtilsShim {
+    protected SocketUtilsShimImpl() {}
+
+    /**
+     * Get a new instance of {@link SocketUtilsShim}.
+     *
+     * Use com.android.networkstack.apishim.SocketUtilsShim#newInstance()
+     * (non-API29 version) instead, to use the correct shims depending on build SDK.
+     */
+    public static SocketUtilsShim newInstance() {
+        return new SocketUtilsShimImpl();
+    }
+
     @NonNull
     @Override
     public SocketAddress makePacketSocketAddress(
diff --git a/apishim/current/com/android/networkstack/apishim/SocketUtilsShimImpl.java b/apishim/30/com/android/networkstack/apishim/SocketUtilsShimImpl.java
similarity index 68%
rename from apishim/current/com/android/networkstack/apishim/SocketUtilsShimImpl.java
rename to apishim/30/com/android/networkstack/apishim/SocketUtilsShimImpl.java
index 92f8438..9516e7c 100644
--- a/apishim/current/com/android/networkstack/apishim/SocketUtilsShimImpl.java
+++ b/apishim/30/com/android/networkstack/apishim/SocketUtilsShimImpl.java
@@ -17,6 +17,7 @@
 package com.android.networkstack.apishim;
 
 import android.net.util.SocketUtils;
+import android.os.Build;
 
 import androidx.annotation.NonNull;
 
@@ -25,7 +26,20 @@
 /**
  * Implementation of {@link SocketUtilsShim} for API 30.
  */
-public class SocketUtilsShimImpl implements SocketUtilsShim {
+public class SocketUtilsShimImpl
+        extends com.android.networkstack.apishim.api29.SocketUtilsShimImpl {
+    protected SocketUtilsShimImpl() {}
+
+    /**
+     * Get a new instance of {@link SocketUtilsShim}.
+     */
+    public static SocketUtilsShim newInstance() {
+        if (!ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q)) {
+            return com.android.networkstack.apishim.api29.SocketUtilsShimImpl.newInstance();
+        }
+        return new SocketUtilsShimImpl();
+    }
+
     @NonNull
     @Override
     public SocketAddress makePacketSocketAddress(
diff --git a/apishim/common/com/android/networkstack/apishim/ShimUtils.java b/apishim/common/com/android/networkstack/apishim/ShimUtils.java
new file mode 100644
index 0000000..e9b5305
--- /dev/null
+++ b/apishim/common/com/android/networkstack/apishim/ShimUtils.java
@@ -0,0 +1,45 @@
+/*
+ * 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 com.android.networkstack.apishim;
+
+import android.os.Build;
+
+/**
+ * Utility class for API shims.
+ */
+public final class ShimUtils {
+    /**
+     * Check whether the device release or development API level is strictly higher than the passed
+     * in level.
+     *
+     * On a development build (codename != REL), the device will have the same API level as the
+     * last stable release, even though some additional APIs may be available. In this method the
+     * device API level is considered to be higher if the device supports a stable SDK with a higher
+     * version number, or if the device supports a development version of a SDK that has a higher
+     * version number.
+     *
+     * @return True if the device supports an SDK that has or will have a higher version number,
+     *         even if still in development.
+     */
+    public static boolean isReleaseOrDevelopmentApiAbove(int apiLevel) {
+        // In-development API n+1 will have SDK_INT == n and CODENAME != REL.
+        // Stable API n has SDK_INT == n and CODENAME == REL.
+        final int devApiLevel = Build.VERSION.SDK_INT
+                + ("REL".equals(Build.VERSION.CODENAME) ? 0 : 1);
+        return devApiLevel > apiLevel;
+    }
+}
diff --git a/src/com/android/networkstack/apishim/SocketUtilsShim.java b/apishim/common/com/android/networkstack/apishim/SocketUtilsShim.java
similarity index 72%
rename from src/com/android/networkstack/apishim/SocketUtilsShim.java
rename to apishim/common/com/android/networkstack/apishim/SocketUtilsShim.java
index 34b5f40..33cb5f8 100644
--- a/src/com/android/networkstack/apishim/SocketUtilsShim.java
+++ b/apishim/common/com/android/networkstack/apishim/SocketUtilsShim.java
@@ -30,18 +30,6 @@
  */
 public interface SocketUtilsShim {
     /**
-     * Create a new instance of SocketUtilsShim.
-     */
-    @NonNull
-    static SocketUtilsShim newInstance() {
-        // TODO: when the R API is finalized, rename the API 29 shim to SocketUtilsCompat, and
-        // return it here instead of SocketUtilsShimImpl for devices with Build.VERSION <= 29.
-        // For now, the switch between implementations is done at build time (swapping the java file
-        // with another), since production modules should not be built with a non-finalized API.
-        return new SocketUtilsShimImpl();
-    }
-
-    /**
      * @see android.net.util.SocketUtils#makePacketSocketAddress(int, int, byte[])
      */
     @NonNull
diff --git a/apishim/jarjar-rules-compat.txt b/apishim/jarjar-rules-compat.txt
new file mode 100644
index 0000000..3e54c12
--- /dev/null
+++ b/apishim/jarjar-rules-compat.txt
@@ -0,0 +1,7 @@
+# jarjar rules to use on API stable builds.
+# Use the latest stable apishim package as the main apishim package, to replace and avoid building
+# the unstable, non-compatibility shims.
+# Once API 30 is stable, apishim/30/com.android.networkstack.apishim should be moved to the
+# com.android.networkstack.apishim.api30 package, a new apishim/31/com.android.networkstack.apishim
+# package should be created, and this rule should reference api30.
+rule com.android.networkstack.apishim.api29.** com.android.networkstack.apishim.@1
\ No newline at end of file
diff --git a/common/moduleutils/Android.bp b/common/moduleutils/Android.bp
new file mode 100644
index 0000000..6117149
--- /dev/null
+++ b/common/moduleutils/Android.bp
@@ -0,0 +1,21 @@
+//
+// 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.
+//
+
+// Shared utility sources to be used by multiple network modules
+filegroup {
+    name: "net-module-utils-srcs",
+    srcs: ["src/**/*.java"],
+}
\ No newline at end of file
diff --git a/src/android/net/util/SharedLog.java b/common/moduleutils/src/android/net/util/SharedLog.java
similarity index 100%
rename from src/android/net/util/SharedLog.java
rename to common/moduleutils/src/android/net/util/SharedLog.java