From 1006a47b29b1a8e5fe2970c96f25eb101daf37d4 Mon Sep 17 00:00:00 2001 From: Remi NGUYEN VAN Date: Thu, 11 Jun 2020 06:31:14 +0000 Subject: [PATCH] Remove IpClientCallbacks dependency on DhcpResults DhcpResults should only be used inside the NetworkStack: modules are expected to use DhcpResultsParcelable instead. Add a compat utility so current users of DhcpResults can do the conversion (wifi in AOSP in particular). The utility can be removed when changes to stop depending on DhcpResults are merged in AOSP. Bug: 149403767 Test: built, flashed, WiFi working Original-Change: https://android-review.googlesource.com/1277608 Merged-In: I5b85c1a541ecdf9dd3e9403b9fb1c2b5aba98dd8 Change-Id: I5b85c1a541ecdf9dd3e9403b9fb1c2b5aba98dd8 --- .../android/net/ip/IpClientCallbacks.java | 23 ++++---- .../net/java/android/net/ip/IpClientUtil.java | 3 -- .../net/util/DhcpResultsCompatUtil.java | 54 +++++++++++++++++++ 3 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 services/net/java/android/net/util/DhcpResultsCompatUtil.java diff --git a/services/net/java/android/net/ip/IpClientCallbacks.java b/services/net/java/android/net/ip/IpClientCallbacks.java index c93e5c5e4759c..b172c4be7b0d3 100644 --- a/services/net/java/android/net/ip/IpClientCallbacks.java +++ b/services/net/java/android/net/ip/IpClientCallbacks.java @@ -16,7 +16,6 @@ package android.net.ip; -import android.net.DhcpResults; import android.net.DhcpResultsParcelable; import android.net.Layer2PacketParcelable; import android.net.LinkProperties; @@ -67,19 +66,15 @@ public class IpClientCallbacks { *

DHCPv4 or static IPv4 configuration failure or success can be determined by whether or not * the passed-in DhcpResults object is null. */ - public void onNewDhcpResults(DhcpResults dhcpResults) {} - - /** - * Callback called when new DHCP results are available. - * - *

This is purely advisory and not an indication of provisioning success or failure. This is - * only here for callers that want to expose DHCPv4 results to other APIs - * (e.g., WifiInfo#setInetAddress). - * - *

DHCPv4 or static IPv4 configuration failure or success can be determined by whether or not - * the passed-in DhcpResults object is null. - */ - public void onNewDhcpResults(DhcpResultsParcelable dhcpResults) {} + public void onNewDhcpResults(DhcpResultsParcelable dhcpResults) { + // In general callbacks would not use a parcelable directly (DhcpResultsParcelable), and + // would use a wrapper instead. But there are already two classes in the tree for DHCP + // information: DhcpInfo and DhcpResults, and each of them do not expose an appropriate API + // (they are bags of mutable fields and can't be changed because they are public API and + // @UnsupportedAppUsage). Adding a third class would cost more than the gain considering + // that the only client of this callback is WiFi, which will end up converting the results + // to DhcpInfo anyway. + } /** * Indicates that provisioning was successful. diff --git a/services/net/java/android/net/ip/IpClientUtil.java b/services/net/java/android/net/ip/IpClientUtil.java index b329aeec4853b..426614ec2f530 100644 --- a/services/net/java/android/net/ip/IpClientUtil.java +++ b/services/net/java/android/net/ip/IpClientUtil.java @@ -16,8 +16,6 @@ package android.net.ip; -import static android.net.shared.IpConfigurationParcelableUtil.fromStableParcelable; - import android.content.Context; import android.net.DhcpResultsParcelable; import android.net.Layer2PacketParcelable; @@ -118,7 +116,6 @@ public class IpClientUtil { // null or not. @Override public void onNewDhcpResults(DhcpResultsParcelable dhcpResults) { - mCb.onNewDhcpResults(fromStableParcelable(dhcpResults)); mCb.onNewDhcpResults(dhcpResults); } diff --git a/services/net/java/android/net/util/DhcpResultsCompatUtil.java b/services/net/java/android/net/util/DhcpResultsCompatUtil.java new file mode 100644 index 0000000000000..fce0834c116e3 --- /dev/null +++ b/services/net/java/android/net/util/DhcpResultsCompatUtil.java @@ -0,0 +1,54 @@ +/* + * 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 android.net.util; + +import static android.net.shared.IpConfigurationParcelableUtil.unparcelAddress; + +import android.annotation.Nullable; +import android.net.DhcpResults; +import android.net.DhcpResultsParcelable; + +import java.net.Inet4Address; + +/** + * Compatibility utility for code that still uses DhcpResults. + * + * TODO: remove this class when all usages of DhcpResults (including Wifi in AOSP) are removed. + */ +public class DhcpResultsCompatUtil { + + /** + * Convert a DhcpResultsParcelable to DhcpResults. + * + * contract { + * returns(null) implies p == null + * returnsNotNull() implies p != null + * } + */ + @Nullable + public static DhcpResults fromStableParcelable(@Nullable DhcpResultsParcelable p) { + if (p == null) return null; + final DhcpResults results = new DhcpResults(p.baseConfiguration); + results.leaseDuration = p.leaseDuration; + results.mtu = p.mtu; + results.serverAddress = (Inet4Address) unparcelAddress(p.serverAddress); + results.vendorInfo = p.vendorInfo; + results.serverHostName = p.serverHostName; + results.captivePortalApiUrl = p.captivePortalApiUrl; + return results; + } +}