Merge "Update apex_available field" am: 94655a9ced am: 8803ad7786

Original change: https://android-review.googlesource.com/c/platform/packages/modules/NetworkStack/+/1396507

Change-Id: Ib1d18dc95f7fdaf048ea95ddcee0d03032567e65
diff --git a/Android.bp b/Android.bp
index cdda32a..314def9 100644
--- a/Android.bp
+++ b/Android.bp
@@ -48,8 +48,7 @@
 
 java_defaults {
     name: "NetworkStackReleaseApiLevel",
-    // TODO: use system_30 once a prebuilt SDK is available for R
-    sdk_version: "system_current",
+    sdk_version: "system_30",
     min_sdk_version: "29",
     target_sdk_version: "30",
 }
@@ -92,6 +91,7 @@
         "netlink-client",
         "networkstack-client",
         "net-utils-framework-common",
+        // See note on statsprotos when adding/updating proto build rules
         "datastallprotosnano",
         "statsprotos",
         "captiveportal-lib",
@@ -129,7 +129,7 @@
     defaults: ["NetworkStackReleaseApiLevel"],
     srcs: [
         "src/**/*.java",
-        ":statslog-networkstack-java-gen-q",
+        ":statslog-networkstack-java-gen",
     ],
     // API stable uses a jarjared version of the shims
     static_libs: [
@@ -255,15 +255,6 @@
 }
 
 genrule {
-    name: "statslog-networkstack-java-gen-q",
-    tools: ["stats-log-api-gen"],
-    cmd: "$(location stats-log-api-gen) --java $(out) --module network_stack" +
-         " --javaPackage com.android.networkstack.metrics --javaClass NetworkStackStatsLog" +
-         " --compileQ",
-    out: ["com/android/networkstack/metrics/NetworkStackStatsLog.java"],
-}
-
-genrule {
     name: "statslog-networkstack-java-gen",
     tools: ["stats-log-api-gen"],
     cmd: "$(location stats-log-api-gen) --java $(out) --module network_stack" +
@@ -295,6 +286,8 @@
     required: ["NetworkPermissionConfig"],
 }
 
+// When adding or modifying protos, the jarjar rules and possibly proguard rules need
+// to be updated: proto libraries may pull additional static libraries.
 java_library_static {
     name: "statsprotos",
     proto: {
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 1cae2c2..879e7ed 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -19,7 +19,7 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.android.networkstack"
   android:sharedUserId="android.uid.networkstack"
-  android:versionCode="299900000"
+  android:versionCode="300000000"
   android:versionName="2019-09"
 >
     <!-- Permissions must be defined here, and not in the base manifest, as the network stack
diff --git a/common/networkstackclient/Android.bp b/common/networkstackclient/Android.bp
index f6b97ff..0476add 100644
--- a/common/networkstackclient/Android.bp
+++ b/common/networkstackclient/Android.bp
@@ -27,6 +27,7 @@
         java: {
             apex_available: [
                 "//apex_available:platform",
+                "com.android.wifi",
                 "com.android.bluetooth.updatable",
                 "com.android.tethering",
             ],
@@ -92,6 +93,7 @@
             apex_available: [
                 "//apex_available:platform",
                 "com.android.bluetooth.updatable",
+                "com.android.wifi",
                 "com.android.tethering",
             ],
             // this is part of updatable modules(NetworkStack) which targets 29(Q)
diff --git a/jarjar-rules-shared.txt b/jarjar-rules-shared.txt
index 24dc4fb..e766b5f 100644
--- a/jarjar-rules-shared.txt
+++ b/jarjar-rules-shared.txt
@@ -2,6 +2,7 @@
 rule com.android.net.module.util.** com.android.networkstack.util.@1
 
 rule com.android.internal.util.** android.net.networkstack.util.@1
+rule com.google.protobuf.** com.android.networkstack.protobuf.@1
 
 # Classes from net-utils-framework-common
 rule com.android.net.module.util.** com.android.networkstack.util.@1
diff --git a/proguard.flags b/proguard.flags
index c60f6c3..af4262a 100644
--- a/proguard.flags
+++ b/proguard.flags
@@ -7,3 +7,10 @@
     static final int CMD_*;
     static final int EVENT_*;
 }
+
+# The lite proto runtime uses reflection to access fields based on the names in
+# the schema, keep all the fields.
+# This replicates the base proguard rule used by the build by default
+# (proguard_basic_keeps.flags), but needs to be specified here because the
+# com.google.protobuf package is jarjared to the below package.
+-keepclassmembers class * extends com.android.networkstack.protobuf.MessageLite { <fields>; }
diff --git a/src/android/net/ip/IpClientLinkObserver.java b/src/android/net/ip/IpClientLinkObserver.java
index 4a5ff78..5fd45a3 100644
--- a/src/android/net/ip/IpClientLinkObserver.java
+++ b/src/android/net/ip/IpClientLinkObserver.java
@@ -161,8 +161,12 @@
             // now empty. Note that from the moment that the interface is removed, any further
             // interface-specific messages (e.g., RTM_DELADDR) will not reach us, because the netd
             // code that parses them will not be able to resolve the ifindex to an interface name.
-            clearLinkProperties();
-            mCallback.update(getInterfaceLinkState());
+            final boolean linkState;
+            synchronized (this) {
+                clearLinkProperties();
+                linkState = getInterfaceLinkStateLocked();
+            }
+            mCallback.update(linkState);
         }
     }
 
@@ -170,7 +174,9 @@
     public void onInterfaceLinkStateChanged(String iface, boolean state) {
         if (mInterfaceName.equals(iface)) {
             maybeLog("interfaceLinkStateChanged", iface + (state ? " up" : " down"));
-            setInterfaceLinkState(state);
+            synchronized (this) {
+                setInterfaceLinkStateLocked(state);
+            }
         }
     }
 
@@ -178,12 +184,14 @@
     public void onInterfaceAddressUpdated(LinkAddress address, String iface) {
         if (mInterfaceName.equals(iface)) {
             maybeLog("addressUpdated", iface, address);
-            boolean changed;
+            final boolean changed;
+            final boolean linkState;
             synchronized (this) {
                 changed = mLinkProperties.addLinkAddress(address);
+                linkState = getInterfaceLinkStateLocked();
             }
             if (changed) {
-                mCallback.update(getInterfaceLinkState());
+                mCallback.update(linkState);
             }
         }
     }
@@ -192,12 +200,14 @@
     public void onInterfaceAddressRemoved(LinkAddress address, String iface) {
         if (mInterfaceName.equals(iface)) {
             maybeLog("addressRemoved", iface, address);
-            boolean changed;
+            final boolean changed;
+            final boolean linkState;
             synchronized (this) {
                 changed = mLinkProperties.removeLinkAddress(address);
+                linkState = getInterfaceLinkStateLocked();
             }
             if (changed) {
-                mCallback.update(getInterfaceLinkState());
+                mCallback.update(linkState);
             }
         }
     }
@@ -206,12 +216,14 @@
     public void onRouteUpdated(RouteInfo route) {
         if (mInterfaceName.equals(route.getInterface())) {
             maybeLog("routeUpdated", route);
-            boolean changed;
+            final boolean changed;
+            final boolean linkState;
             synchronized (this) {
                 changed = mLinkProperties.addRoute(route);
+                linkState = getInterfaceLinkStateLocked();
             }
             if (changed) {
-                mCallback.update(getInterfaceLinkState());
+                mCallback.update(linkState);
             }
         }
     }
@@ -220,12 +232,14 @@
     public void onRouteRemoved(RouteInfo route) {
         if (mInterfaceName.equals(route.getInterface())) {
             maybeLog("routeRemoved", route);
-            boolean changed;
+            final boolean changed;
+            final boolean linkState;
             synchronized (this) {
                 changed = mLinkProperties.removeRoute(route);
+                linkState = getInterfaceLinkStateLocked();
             }
             if (changed) {
-                mCallback.update(getInterfaceLinkState());
+                mCallback.update(linkState);
             }
         }
     }
@@ -234,12 +248,14 @@
     public void onInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
         if (mInterfaceName.equals(iface)) {
             maybeLog("interfaceDnsServerInfo", Arrays.toString(addresses));
-            boolean changed = mDnsServerRepository.addServers(lifetime, addresses);
+            final boolean changed = mDnsServerRepository.addServers(lifetime, addresses);
+            final boolean linkState;
             if (changed) {
                 synchronized (this) {
                     mDnsServerRepository.setDnsServersOn(mLinkProperties);
+                    linkState = getInterfaceLinkStateLocked();
                 }
-                mCallback.update(getInterfaceLinkState());
+                mCallback.update(linkState);
             }
         }
     }
@@ -264,11 +280,11 @@
         mLinkProperties.setInterfaceName(mInterfaceName);
     }
 
-    private synchronized boolean getInterfaceLinkState() {
+    private boolean getInterfaceLinkStateLocked() {
         return mInterfaceLinkState;
     }
 
-    private synchronized void setInterfaceLinkState(boolean state) {
+    private void setInterfaceLinkStateLocked(boolean state) {
         mInterfaceLinkState = state;
     }
 
@@ -384,7 +400,7 @@
                 cancelPref64Alarm();
             }
 
-            mCallback.update(getInterfaceLinkState());
+            mCallback.update(getInterfaceLinkStateLocked());
         }
 
         private void processPref64Option(StructNdOptPref64 opt, final long now) {
diff --git a/src/com/android/networkstack/NetworkStackNotifier.java b/src/com/android/networkstack/NetworkStackNotifier.java
index 872834a..dbb62b1 100644
--- a/src/com/android/networkstack/NetworkStackNotifier.java
+++ b/src/com/android/networkstack/NetworkStackNotifier.java
@@ -239,7 +239,7 @@
                     .setContentText(res.getString(R.string.tap_for_info))
                     .setContentIntent(mDependencies.getActivityPendingIntent(
                             getContextAsUser(mContext, UserHandle.CURRENT),
-                            infoIntent, PendingIntent.FLAG_UPDATE_CURRENT));
+                            infoIntent, PendingIntent.FLAG_IMMUTABLE));
 
             networkStatus.mShownNotification = NOTE_VENUE_INFO;
         } else if (showValidated) {
@@ -252,7 +252,7 @@
                     .setContentIntent(mDependencies.getActivityPendingIntent(
                             getContextAsUser(mContext, UserHandle.CURRENT),
                             new Intent(Settings.ACTION_WIFI_SETTINGS),
-                            PendingIntent.FLAG_UPDATE_CURRENT));
+                            PendingIntent.FLAG_IMMUTABLE));
 
             networkStatus.mShownNotification = NOTE_CONNECTED;
         } else {
diff --git a/src/com/android/networkstack/metrics/DataStallDetectionStats.java b/src/com/android/networkstack/metrics/DataStallDetectionStats.java
index 47f5805..131fb79 100644
--- a/src/com/android/networkstack/metrics/DataStallDetectionStats.java
+++ b/src/com/android/networkstack/metrics/DataStallDetectionStats.java
@@ -261,11 +261,13 @@
             if (info == null) return DataStallEventProto.AP_BAND_UNKNOWN;
 
             int freq = info.getFrequency();
-            // Refer to ScanResult.is5GHz() and ScanResult.is24GHz().
-            if (freq > 4900 && freq < 5900) {
+            // Refer to ScanResult.is5GHz(), ScanResult.is24GHz() and ScanResult.is6GHz().
+            if (freq >= 5160 && freq <= 5865) {
                 return DataStallEventProto.AP_BAND_5GHZ;
-            } else if (freq > 2400 && freq < 2500) {
+            } else if (freq >= 2412 && freq <= 2484) {
                 return DataStallEventProto.AP_BAND_2GHZ;
+            } else if (freq >= 5945 && freq <= 7105) {
+                return DataStallEventProto.AP_BAND_6GHZ;
             } else {
                 return DataStallEventProto.AP_BAND_UNKNOWN;
             }
diff --git a/tests/integration/Android.bp b/tests/integration/Android.bp
index 1a985a9..af63f0e 100644
--- a/tests/integration/Android.bp
+++ b/tests/integration/Android.bp
@@ -82,6 +82,18 @@
     test_suites: ["device-tests"],
 }
 
+// The static lib needs to be jarjared by each module so they do not conflict with each other
+// (e.g. wifi, system server, network stack need to use different package names when including it).
+// Apply NetworkStack jarjar rules to the tests as well so classes in NetworkStaticLibTests have the
+// same package names as in module code.
+android_library {
+    name: "NetworkStackStaticLibTestsLib",
+    platform_apis: true,
+    min_sdk_version: "29",
+    jarjar_rules: ":NetworkStackJarJarRules",
+    static_libs: ["NetworkStaticLibTestsLib"],
+}
+
 // Special version of the network stack tests that includes all tests necessary for code coverage
 // purposes. This is currently the union of NetworkStackTests and NetworkStackIntegrationTests.
 android_test {
@@ -95,7 +107,7 @@
     static_libs: [
         "NetworkStackTestsLib",
         "NetworkStackIntegrationTestsLib",
-        "NetworkStaticLibTestsLib",
+        "NetworkStackStaticLibTestsLib",
     ],
     compile_multilib: "both",
     manifest: "AndroidManifest_coverage.xml",
diff --git a/tests/integration/src/android/net/ip/IpClientIntegrationTest.java b/tests/integration/src/android/net/ip/IpClientIntegrationTest.java
index b7cd6b3..20b738f 100644
--- a/tests/integration/src/android/net/ip/IpClientIntegrationTest.java
+++ b/tests/integration/src/android/net/ip/IpClientIntegrationTest.java
@@ -149,7 +149,6 @@
 
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -1513,7 +1512,6 @@
 
     }
 
-    @Ignore  // AOSP kernels don't support the PREF64 option yet.
     @Test @IgnoreUpTo(Build.VERSION_CODES.Q)
     public void testPref64Option() throws Exception {
         assumeTrue(ConstantsShim.VERSION > Build.VERSION_CODES.Q);
diff --git a/tests/unit/src/com/android/networkstack/NetworkStackNotifierTest.kt b/tests/unit/src/com/android/networkstack/NetworkStackNotifierTest.kt
index b2607eb..348392d 100644
--- a/tests/unit/src/com/android/networkstack/NetworkStackNotifierTest.kt
+++ b/tests/unit/src/com/android/networkstack/NetworkStackNotifierTest.kt
@@ -22,7 +22,7 @@
 import android.app.NotificationManager.IMPORTANCE_DEFAULT
 import android.app.NotificationManager.IMPORTANCE_NONE
 import android.app.PendingIntent
-import android.app.PendingIntent.FLAG_UPDATE_CURRENT
+import android.app.PendingIntent.FLAG_IMMUTABLE
 import android.content.Context
 import android.content.Intent
 import android.content.res.Resources
@@ -57,6 +57,7 @@
 import org.mockito.ArgumentCaptor
 import org.mockito.ArgumentMatchers.anyInt
 import org.mockito.ArgumentMatchers.eq
+import org.mockito.ArgumentMatchers.intThat
 import org.mockito.Captor
 import org.mockito.Mock
 import org.mockito.Mockito.any
@@ -188,7 +189,8 @@
         assertEquals(CHANNEL_CONNECTED, note.channelId)
         assertEquals(timeout, note.timeoutAfter)
         verify(mDependencies).getActivityPendingIntent(
-                eq(mCurrentUserContext), mIntentCaptor.capture(), eq(FLAG_UPDATE_CURRENT))
+                eq(mCurrentUserContext), mIntentCaptor.capture(),
+                intThat { it or FLAG_IMMUTABLE != 0 })
     }
 
     private fun verifyCanceledNotificationAfterNetworkLost() {
@@ -279,7 +281,8 @@
 
         verify(mNm).notify(eq(TEST_NETWORK_TAG), mNoteIdCaptor.capture(), mNoteCaptor.capture())
         verify(mDependencies).getActivityPendingIntent(
-                eq(mCurrentUserContext), mIntentCaptor.capture(), eq(FLAG_UPDATE_CURRENT))
+                eq(mCurrentUserContext), mIntentCaptor.capture(),
+                intThat { it or FLAG_IMMUTABLE != 0 })
         verifyVenueInfoIntent(mIntentCaptor.value)
         verifyCanceledNotificationAfterDefaultNetworkLost()
     }