summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRemi NGUYEN VAN <reminv@google.com>2019-12-18 20:57:47 +0900
committerRemi NGUYEN VAN <reminv@google.com>2020-02-14 15:10:01 +0900
commit8282d3c8f1c152f171099b617341f89444a2d92d (patch)
tree40f3cd42205423e2a0f5ae189207d7734fb83262
parent5365684ad59e25d3633fc0f5b2a8328cddc0a235 (diff)
Add shims for network info classes
Combine shims for LinkProperties and NetworkCapabilities to avoid too many shim classes, and use static methods as the original types (and not the shims) would generally be used to reference the classes. CaptivePortalData is not available in API29 so CaptivePortalDataShim is used as a wrapper that can hold a reference to it in common code. Sample usage in NetworkMonitor: final CaptivePortalDataShim data; try { data = CaptivePortalDataShimImpl.fromJSON(obj); } catch (UnsupportedApiLevelException | JSONException e) { // Do some fallback return; } // Just to give an idea if (data.isCaptive()) { openCaptivePortalApp(data.getUserPortalUrl()); } data.notifyChanged(mCallbacks); Test: atest NetworkStackTests NetworkStackNextTests Bug: 139269711 Change-Id: I262aaa41013ebe1ec4263a6516bd8cab76509304
-rw-r--r--apishim/29/com/android/networkstack/apishim/api29/CaptivePortalDataShimImpl.java49
-rw-r--r--apishim/29/com/android/networkstack/apishim/api29/NetworkInformationShimImpl.java65
-rw-r--r--apishim/30/com/android/networkstack/apishim/CaptivePortalDataShimImpl.java96
-rw-r--r--apishim/30/com/android/networkstack/apishim/NetworkInformationShimImpl.java62
-rw-r--r--apishim/common/com/android/networkstack/apishim/CaptivePortalDataShim.java41
-rw-r--r--apishim/common/com/android/networkstack/apishim/NetworkInformationShim.java47
-rw-r--r--common/networkstackclient/src/android/net/INetworkMonitorCallbacks.aidl2
7 files changed, 362 insertions, 0 deletions
diff --git a/apishim/29/com/android/networkstack/apishim/api29/CaptivePortalDataShimImpl.java b/apishim/29/com/android/networkstack/apishim/api29/CaptivePortalDataShimImpl.java
new file mode 100644
index 0000000..d6e6b53
--- /dev/null
+++ b/apishim/29/com/android/networkstack/apishim/api29/CaptivePortalDataShimImpl.java
@@ -0,0 +1,49 @@
+/*
+ * 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.api29;
+
+import androidx.annotation.NonNull;
+
+import com.android.networkstack.apishim.CaptivePortalDataShim;
+import com.android.networkstack.apishim.UnsupportedApiLevelException;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+/**
+ * Compatibility implementation of {@link CaptivePortalDataShim}.
+ *
+ * <p>Use {@link com.android.networkstack.apishim.CaptivePortalDataShimImpl} instead of this
+ * fallback implementation.
+ */
+public abstract class CaptivePortalDataShimImpl implements CaptivePortalDataShim {
+ protected CaptivePortalDataShimImpl() {}
+
+ /**
+ * Parse a {@link android.net.CaptivePortalData} from JSON.
+ *
+ * <p>Use
+ * {@link com.android.networkstack.apishim.CaptivePortalDataShimImpl#fromJson(JSONObject)}
+ * instead of this API 29 compatibility version.
+ */
+ @NonNull
+ public static CaptivePortalDataShim fromJson(JSONObject object) throws JSONException,
+ UnsupportedApiLevelException {
+ // Data class not supported in API 29
+ throw new UnsupportedApiLevelException("CaptivePortalData not supported on API 29");
+ }
+}
diff --git a/apishim/29/com/android/networkstack/apishim/api29/NetworkInformationShimImpl.java b/apishim/29/com/android/networkstack/apishim/api29/NetworkInformationShimImpl.java
new file mode 100644
index 0000000..a500a71
--- /dev/null
+++ b/apishim/29/com/android/networkstack/apishim/api29/NetworkInformationShimImpl.java
@@ -0,0 +1,65 @@
+/*
+ * 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.api29;
+
+import android.net.LinkProperties;
+import android.net.NetworkCapabilities;
+import android.net.Uri;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import com.android.networkstack.apishim.NetworkInformationShim;
+
+/**
+ * Compatibility implementation of {@link NetworkInformationShim}.
+ *
+ * <p>Use {@link com.android.networkstack.apishim.NetworkInformationShimImpl} instead of this
+ * fallback implementation.
+ */
+public class NetworkInformationShimImpl implements NetworkInformationShim {
+ protected NetworkInformationShimImpl() {}
+
+ /**
+ * Get a new instance of {@link NetworkInformationShim}.
+ *
+ * <p>Use com.android.networkstack.apishim.LinkPropertiesShimImpl#newInstance()
+ * (non-API29 version) instead, to use the correct shims depending on build SDK.
+ */
+ public static NetworkInformationShim newInstance() {
+ return new NetworkInformationShimImpl();
+ }
+
+ @Nullable
+ @Override
+ public Uri getCaptivePortalApiUrl(@Nullable LinkProperties lp) {
+ // Not supported on this API level
+ return null;
+ }
+
+ @Override
+ public void setCaptivePortalApiUrl(@NonNull LinkProperties lp, @Nullable Uri url) {
+ // Not supported on this API level: no-op
+ }
+
+ @Nullable
+ @Override
+ public String getSSID(@Nullable NetworkCapabilities nc) {
+ // Not supported on this API level
+ return null;
+ }
+}
diff --git a/apishim/30/com/android/networkstack/apishim/CaptivePortalDataShimImpl.java b/apishim/30/com/android/networkstack/apishim/CaptivePortalDataShimImpl.java
new file mode 100644
index 0000000..6d3fb88
--- /dev/null
+++ b/apishim/30/com/android/networkstack/apishim/CaptivePortalDataShimImpl.java
@@ -0,0 +1,96 @@
+/*
+ * 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.net.CaptivePortalData;
+import android.net.INetworkMonitorCallbacks;
+import android.net.Uri;
+import android.os.Build;
+import android.os.RemoteException;
+
+import androidx.annotation.NonNull;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+/**
+ * Compatibility implementation of {@link CaptivePortalDataShim}.
+ */
+public class CaptivePortalDataShimImpl
+ extends com.android.networkstack.apishim.api29.CaptivePortalDataShimImpl {
+ @NonNull
+ private final CaptivePortalData mData;
+
+ protected CaptivePortalDataShimImpl(@NonNull CaptivePortalData data) {
+ mData = data;
+ }
+
+ /**
+ * Parse a {@link CaptivePortalDataShim} from a JSON object.
+ * @throws JSONException The JSON is not a representation of correct captive portal data.
+ * @throws UnsupportedApiLevelException CaptivePortalData is not available on this API level.
+ */
+ @NonNull
+ public static CaptivePortalDataShim fromJson(JSONObject obj) throws JSONException,
+ UnsupportedApiLevelException {
+ if (!ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q)) {
+ return com.android.networkstack.apishim.api29.CaptivePortalDataShimImpl.fromJson(obj);
+ }
+
+ final long refreshTimeMs = System.currentTimeMillis();
+ final long secondsRemaining = getLongOrDefault(obj, "seconds-remaining", -1L);
+ final long millisRemaining = secondsRemaining <= Long.MAX_VALUE / 1000
+ ? secondsRemaining * 1000
+ : Long.MAX_VALUE;
+ final long expiryTimeMs = secondsRemaining == -1L ? -1L :
+ refreshTimeMs + Math.min(Long.MAX_VALUE - refreshTimeMs, millisRemaining);
+ return new CaptivePortalDataShimImpl(new CaptivePortalData.Builder()
+ .setRefreshTime(refreshTimeMs)
+ // captive is mandatory; throws JSONException if absent
+ .setCaptive(obj.getBoolean("captive"))
+ .setUserPortalUrl(getUriOrNull(obj, "user-portal-url"))
+ .setVenueInfoUrl(getUriOrNull(obj, "venue-info-url"))
+ .setBytesRemaining(getLongOrDefault(obj, "bytes-remaining", -1L))
+ .setExpiryTime(expiryTimeMs)
+ .build());
+ }
+
+ private static long getLongOrDefault(JSONObject o, String key, long def) throws JSONException {
+ if (!o.has(key)) return def;
+ return o.getLong(key);
+ }
+
+ private static Uri getUriOrNull(JSONObject o, String key) throws JSONException {
+ if (!o.has(key)) return null;
+ return Uri.parse(o.getString(key));
+ }
+
+ @Override
+ public boolean isCaptive() {
+ return mData.isCaptive();
+ }
+
+ @Override
+ public Uri getUserPortalUrl() {
+ return mData.getUserPortalUrl();
+ }
+
+ @Override
+ public void notifyChanged(INetworkMonitorCallbacks cb) throws RemoteException {
+ cb.notifyCaptivePortalDataChanged(mData);
+ }
+}
diff --git a/apishim/30/com/android/networkstack/apishim/NetworkInformationShimImpl.java b/apishim/30/com/android/networkstack/apishim/NetworkInformationShimImpl.java
new file mode 100644
index 0000000..785f5c4
--- /dev/null
+++ b/apishim/30/com/android/networkstack/apishim/NetworkInformationShimImpl.java
@@ -0,0 +1,62 @@
+/*
+ * 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.net.LinkProperties;
+import android.net.NetworkCapabilities;
+import android.net.Uri;
+import android.os.Build;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+/**
+ * Compatibility implementation of {@link NetworkInformationShim}.
+ */
+public class NetworkInformationShimImpl extends
+ com.android.networkstack.apishim.api29.NetworkInformationShimImpl {
+ protected NetworkInformationShimImpl() {}
+
+ /**
+ * Get a new instance of {@link NetworkInformationShim}.
+ */
+ public static NetworkInformationShim newInstance() {
+ if (!ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q)) {
+ return com.android.networkstack.apishim.api29.NetworkInformationShimImpl.newInstance();
+ }
+ return new NetworkInformationShimImpl();
+ }
+
+ @Nullable
+ @Override
+ public Uri getCaptivePortalApiUrl(@Nullable LinkProperties lp) {
+ if (lp == null) return null;
+ return lp.getCaptivePortalApiUrl();
+ }
+
+ @Override
+ public void setCaptivePortalApiUrl(@NonNull LinkProperties lp, @Nullable Uri url) {
+ lp.setCaptivePortalApiUrl(url);
+ }
+
+ @Nullable
+ @Override
+ public String getSSID(@Nullable NetworkCapabilities nc) {
+ if (nc == null) return null;
+ return nc.getSSID();
+ }
+}
diff --git a/apishim/common/com/android/networkstack/apishim/CaptivePortalDataShim.java b/apishim/common/com/android/networkstack/apishim/CaptivePortalDataShim.java
new file mode 100644
index 0000000..e460155
--- /dev/null
+++ b/apishim/common/com/android/networkstack/apishim/CaptivePortalDataShim.java
@@ -0,0 +1,41 @@
+/*
+ * 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.net.INetworkMonitorCallbacks;
+import android.net.Uri;
+import android.os.RemoteException;
+
+/**
+ * Compatibility interface for {@link android.net.CaptivePortalData}.
+ */
+public interface CaptivePortalDataShim {
+ /**
+ * @see android.net.CaptivePortalData#isCaptive()
+ */
+ boolean isCaptive();
+
+ /**
+ * @see android.net.CaptivePortalData#getUserPortalUrl()
+ */
+ Uri getUserPortalUrl();
+
+ /**
+ * @see INetworkMonitorCallbacks#notifyCaptivePortalDataChanged(android.net.CaptivePortalData)
+ */
+ void notifyChanged(INetworkMonitorCallbacks cb) throws RemoteException;
+}
diff --git a/apishim/common/com/android/networkstack/apishim/NetworkInformationShim.java b/apishim/common/com/android/networkstack/apishim/NetworkInformationShim.java
new file mode 100644
index 0000000..9018888
--- /dev/null
+++ b/apishim/common/com/android/networkstack/apishim/NetworkInformationShim.java
@@ -0,0 +1,47 @@
+/*
+ * 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.net.LinkProperties;
+import android.net.NetworkCapabilities;
+import android.net.Uri;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+/**
+ * Compatibility interface for network info classes such as {@link LinkProperties} and
+ * {@link NetworkCapabilities}.
+ */
+public interface NetworkInformationShim {
+ /**
+ * @see LinkProperties#getCaptivePortalApiUrl()
+ */
+ @Nullable
+ Uri getCaptivePortalApiUrl(@Nullable LinkProperties lp);
+
+ /**
+ * @see LinkProperties#setCaptivePortalApiUrl(Uri)
+ */
+ void setCaptivePortalApiUrl(@NonNull LinkProperties lp, @Nullable Uri url);
+
+ /**
+ * @see NetworkCapabilities#getSSID()
+ */
+ @Nullable
+ String getSSID(@Nullable NetworkCapabilities nc);
+}
diff --git a/common/networkstackclient/src/android/net/INetworkMonitorCallbacks.aidl b/common/networkstackclient/src/android/net/INetworkMonitorCallbacks.aidl
index e2901dd..f57a8f7 100644
--- a/common/networkstackclient/src/android/net/INetworkMonitorCallbacks.aidl
+++ b/common/networkstackclient/src/android/net/INetworkMonitorCallbacks.aidl
@@ -16,6 +16,7 @@
package android.net;
+import android.net.CaptivePortalData;
import android.net.INetworkMonitor;
import android.net.PrivateDnsConfigParcel;
@@ -33,4 +34,5 @@ oneway interface INetworkMonitorCallbacks {
long timestampMillis, in PersistableBundle extras);
void notifyDataStallSuspected(long timestampMillis, int detectionMethod,
in PersistableBundle extras);
+ void notifyCaptivePortalDataChanged(in CaptivePortalData data);
} \ No newline at end of file