diff options
4 files changed, 207 insertions, 0 deletions
diff --git a/apishim/29/com/android/networkstack/apishim/api29/NetworkShimImpl.java b/apishim/29/com/android/networkstack/apishim/api29/NetworkShimImpl.java new file mode 100644 index 0000000..642cc90 --- /dev/null +++ b/apishim/29/com/android/networkstack/apishim/api29/NetworkShimImpl.java @@ -0,0 +1,55 @@ +/* + * 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.Network; + +import androidx.annotation.NonNull; + +import com.android.networkstack.apishim.NetworkShim; +import com.android.networkstack.apishim.UnsupportedApiLevelException; +/** + * Implementation of NetworkShim for API 29. + */ +public class NetworkShimImpl implements NetworkShim { + @NonNull + protected final Network mNetwork; + + protected NetworkShimImpl(@NonNull Network network) { + mNetwork = network; + } + + /** + * Get a new instance of {@link NetworkShim}. + * + * Use com.android.networkstack.apishim.NetworkShimImpl#newInstance() + * (non-API29 version) instead, to use the correct shims depending on build SDK. + */ + public static NetworkShim newInstance(@NonNull Network network) { + return new NetworkShimImpl(network); + } + + /** + * Get the netId of the network. + * Throw UnsupportedApiLevelException if API is not available in this API level. + */ + @Override + public int getNetId() throws UnsupportedApiLevelException { + // Not supported for API 29. + throw new UnsupportedApiLevelException("Not supported in API 29."); + } +} diff --git a/apishim/30/com/android/networkstack/apishim/NetworkShimImpl.java b/apishim/30/com/android/networkstack/apishim/NetworkShimImpl.java new file mode 100644 index 0000000..305b8a5 --- /dev/null +++ b/apishim/30/com/android/networkstack/apishim/NetworkShimImpl.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; + +import android.net.Network; +import android.os.Build; + +import androidx.annotation.NonNull; + +/** + * Implementation of {@link NetworkShim} for API 30. + */ +public class NetworkShimImpl extends com.android.networkstack.apishim.api29.NetworkShimImpl { + protected NetworkShimImpl(@NonNull Network network) { + super(network); + } + + /** + * Get a new instance of {@link NetworkShim}. + */ + public static NetworkShim newInstance(@NonNull Network network) { + if (!ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q)) { + return com.android.networkstack.apishim.api29.NetworkShimImpl.newInstance(network); + } + return new NetworkShimImpl(network); + } + + /** + * Get the netId of the network. + */ + @Override + public int getNetId() { + return mNetwork.netId; + } +} diff --git a/apishim/common/com/android/networkstack/apishim/NetworkShim.java b/apishim/common/com/android/networkstack/apishim/NetworkShim.java new file mode 100644 index 0000000..4cdde7f --- /dev/null +++ b/apishim/common/com/android/networkstack/apishim/NetworkShim.java @@ -0,0 +1,34 @@ +/* + * 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; + +/** + * Interface used to access API methods in {@link android.net.Network}, with appropriate fallbacks + * if the methods are not yet part of the released API. + * + * <p>This interface makes it easier for callers to use NetworkShimImpl, as it's more obvious what + * methods must be implemented on each API level, and it abstracts from callers the need to + * reference classes that have different implementations (which also does not work well with IDEs). + */ +public interface NetworkShim { + /** + * @see android.net.Network.netId. + * + * Throw UnsupportedApiLevelException if API is not available in the API level. + */ + int getNetId() throws UnsupportedApiLevelException; +} diff --git a/apishim/common/com/android/networkstack/apishim/UnsupportedApiLevelException.java b/apishim/common/com/android/networkstack/apishim/UnsupportedApiLevelException.java new file mode 100644 index 0000000..f302da7 --- /dev/null +++ b/apishim/common/com/android/networkstack/apishim/UnsupportedApiLevelException.java @@ -0,0 +1,69 @@ +/* + * 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 androidx.annotation.Nullable; + +/** + * Signals that the calling API is not supported with device release or development API level + */ +public class UnsupportedApiLevelException extends Exception { + /** + * Constructs an {@code UnsupportedApiLevelException} with {@code null} as its error detail + * message. + */ + public UnsupportedApiLevelException() { + super(); + } + + /** + * Constructs an {@code UnsupportedApiLevelException} with the specified detail message. + * + * @param message The detail message (which is saved for later retrieval by the + * {@link #getMessage()} method) + */ + public UnsupportedApiLevelException(String message) { + super(message); + } + + /** + * Constructs an {@code UnsupportedApiLevelException} with the specified detail message + * and cause. + * + * @param message The detail message (which is saved for later retrieval by the + * {@link #getMessage()} method) + * + * @param cause The cause (which is saved for later retrieval by the {@link #getCause()} + * method). (A null value is permitted, and indicates that the cause is + * nonexistent or unknown.) + */ + public UnsupportedApiLevelException(String message, @Nullable Throwable cause) { + super(message, cause); + } + + /** + * Constructs an {@code UnsupportedApiLevelException} with the specified cause and a + * detail message of {@code (cause==null ? null : cause.toString())} (which typically contains + * the class and detail message of {@code cause}). + * + * @param cause The cause (which is saved for later retrieval by the {@link #getCause()} + * method). (A null value is permitted, and indicates that the cause is + * nonexistent or unknown.) + */ + public UnsupportedApiLevelException(@Nullable Throwable cause) { + super(cause); + } +} |