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/NetworkInformationShimImpl.java 0 → 100644 +65 −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 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; } } apishim/30/com/android/networkstack/apishim/CaptivePortalDataShimImpl.java 0 → 100644 +102 −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 androidx.annotation.VisibleForTesting; 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()); } @VisibleForTesting 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 void notifyChanged(INetworkMonitorCallbacks cb) throws RemoteException { cb.notifyCaptivePortalDataChanged(mData); } } apishim/30/com/android/networkstack/apishim/NetworkInformationShimImpl.java 0 → 100644 +62 −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.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(); } } 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/NetworkInformationShimImpl.java 0 → 100644 +65 −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 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; } }
apishim/30/com/android/networkstack/apishim/CaptivePortalDataShimImpl.java 0 → 100644 +102 −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 androidx.annotation.VisibleForTesting; 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()); } @VisibleForTesting 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 void notifyChanged(INetworkMonitorCallbacks cb) throws RemoteException { cb.notifyCaptivePortalDataChanged(mData); } }
apishim/30/com/android/networkstack/apishim/NetworkInformationShimImpl.java 0 → 100644 +62 −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.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(); } }