summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChiachang Wang <chiachangwang@google.com>2019-12-18 08:40:15 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2019-12-18 08:40:15 +0000
commitc95af409222b327e76b918526ea2f1850ea92530 (patch)
tree1c89a1d2d88ed88a0972dc2f9d067948d36797c2
parent0350709fe9516165970359ab69f5760d62a6a495 (diff)
parent24af3a96fd161b5e7c820abeea6e418573254efe (diff)
Merge "Add NetworkShim"
-rw-r--r--apishim/29/com/android/networkstack/apishim/api29/NetworkShimImpl.java55
-rw-r--r--apishim/30/com/android/networkstack/apishim/NetworkShimImpl.java49
-rw-r--r--apishim/common/com/android/networkstack/apishim/NetworkShim.java34
-rw-r--r--apishim/common/com/android/networkstack/apishim/UnsupportedApiLevelException.java69
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);
+ }
+}