Loading Android.bp +2 −2 Original line number Diff line number Diff line Loading @@ -60,7 +60,7 @@ filegroup { "apishim/common/**/*.java", "apishim/29/**/*.java", "apishim/30/**/*.java", ":net-module-utils-srcs", ":networkstack-module-utils-srcs", ], } Loading @@ -71,7 +71,7 @@ filegroup { srcs: [ "apishim/common/**/*.java", "apishim/29/**/*.java", ":net-module-utils-srcs", ":networkstack-module-utils-srcs", ], } Loading apishim/29/com/android/networkstack/apishim/api29/CaptivePortalDataShimImpl.java 0 → 100644 +55 −0 Original line number Diff line number Diff line /* * 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 androidx.annotation.VisibleForTesting; 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"); } @VisibleForTesting public static boolean isSupported() { return false; } } apishim/29/com/android/networkstack/apishim/api29/ConstantsShim.java +1 −1 Original line number Diff line number Diff line Loading @@ -24,7 +24,7 @@ public class ConstantsShim { public static final int DETECTION_METHOD_DNS_EVENTS = 1; public static final int DETECTION_METHOD_TCP_METRICS = 2; public static final String KEY_DNS_CONSECUTIVE_TIMEOUTS = "dnsConsecutiveTimeouts"; public static final String KEY_NETWORK_PROBES_ATTEMPTED_BITMASK = "networkProbesAttemped"; public static final String KEY_NETWORK_PROBES_ATTEMPTED_BITMASK = "networkProbesAttempted"; public static final String KEY_NETWORK_PROBES_SUCCEEDED_BITMASK = "networkProbesSucceeded"; public static final String KEY_NETWORK_VALIDATION_RESULT = "networkValidationResult"; public static final String KEY_TCP_METRICS_COLLECTION_PERIOD_MILLIS = Loading apishim/29/com/android/networkstack/apishim/api29/NetworkInformationShimImpl.java 0 → 100644 +87 −0 Original line number Diff line number Diff line /* * 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 androidx.annotation.VisibleForTesting; import com.android.networkstack.apishim.CaptivePortalDataShim; 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(); } /** * Indicates whether the shim can use APIs above the Q SDK. */ @VisibleForTesting public static boolean useApiAboveQ() { return false; } @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 CaptivePortalDataShim getCaptivePortalData(@Nullable LinkProperties lp) { return null; } @Nullable @Override public String getSSID(@Nullable NetworkCapabilities nc) { // Not supported on this API level return null; } @NonNull @Override public LinkProperties makeSensitiveFieldsParcelingCopy(@NonNull final LinkProperties lp) { return new LinkProperties(lp); } } apishim/30/com/android/networkstack/apishim/CaptivePortalDataShimImpl.java 0 → 100644 +105 −0 Original line number Diff line number Diff line /* * 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 (!isSupported()) { 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()); } public static boolean isSupported() { return ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q); } 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 Uri getVenueInfoUrl() { return mData.getVenueInfoUrl(); } @Override public void notifyChanged(INetworkMonitorCallbacks cb) throws RemoteException { cb.notifyCaptivePortalDataChanged(mData); } } Loading
Android.bp +2 −2 Original line number Diff line number Diff line Loading @@ -60,7 +60,7 @@ filegroup { "apishim/common/**/*.java", "apishim/29/**/*.java", "apishim/30/**/*.java", ":net-module-utils-srcs", ":networkstack-module-utils-srcs", ], } Loading @@ -71,7 +71,7 @@ filegroup { srcs: [ "apishim/common/**/*.java", "apishim/29/**/*.java", ":net-module-utils-srcs", ":networkstack-module-utils-srcs", ], } Loading
apishim/29/com/android/networkstack/apishim/api29/CaptivePortalDataShimImpl.java 0 → 100644 +55 −0 Original line number Diff line number Diff line /* * 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 androidx.annotation.VisibleForTesting; 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"); } @VisibleForTesting public static boolean isSupported() { return false; } }
apishim/29/com/android/networkstack/apishim/api29/ConstantsShim.java +1 −1 Original line number Diff line number Diff line Loading @@ -24,7 +24,7 @@ public class ConstantsShim { public static final int DETECTION_METHOD_DNS_EVENTS = 1; public static final int DETECTION_METHOD_TCP_METRICS = 2; public static final String KEY_DNS_CONSECUTIVE_TIMEOUTS = "dnsConsecutiveTimeouts"; public static final String KEY_NETWORK_PROBES_ATTEMPTED_BITMASK = "networkProbesAttemped"; public static final String KEY_NETWORK_PROBES_ATTEMPTED_BITMASK = "networkProbesAttempted"; public static final String KEY_NETWORK_PROBES_SUCCEEDED_BITMASK = "networkProbesSucceeded"; public static final String KEY_NETWORK_VALIDATION_RESULT = "networkValidationResult"; public static final String KEY_TCP_METRICS_COLLECTION_PERIOD_MILLIS = Loading
apishim/29/com/android/networkstack/apishim/api29/NetworkInformationShimImpl.java 0 → 100644 +87 −0 Original line number Diff line number Diff line /* * 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 androidx.annotation.VisibleForTesting; import com.android.networkstack.apishim.CaptivePortalDataShim; 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(); } /** * Indicates whether the shim can use APIs above the Q SDK. */ @VisibleForTesting public static boolean useApiAboveQ() { return false; } @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 CaptivePortalDataShim getCaptivePortalData(@Nullable LinkProperties lp) { return null; } @Nullable @Override public String getSSID(@Nullable NetworkCapabilities nc) { // Not supported on this API level return null; } @NonNull @Override public LinkProperties makeSensitiveFieldsParcelingCopy(@NonNull final LinkProperties lp) { return new LinkProperties(lp); } }
apishim/30/com/android/networkstack/apishim/CaptivePortalDataShimImpl.java 0 → 100644 +105 −0 Original line number Diff line number Diff line /* * 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 (!isSupported()) { 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()); } public static boolean isSupported() { return ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q); } 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 Uri getVenueInfoUrl() { return mData.getVenueInfoUrl(); } @Override public void notifyChanged(INetworkMonitorCallbacks cb) throws RemoteException { cb.notifyCaptivePortalDataChanged(mData); } }