diff --git a/core/java/android/net/InternalNetworkManagementException.aidl b/core/java/android/net/InternalNetworkManagementException.aidl new file mode 100644 index 0000000000..dcce706989 --- /dev/null +++ b/core/java/android/net/InternalNetworkManagementException.aidl @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2021 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 android.net; + + parcelable InternalNetworkManagementException; \ No newline at end of file diff --git a/core/java/android/net/InternalNetworkManagementException.java b/core/java/android/net/InternalNetworkManagementException.java new file mode 100644 index 0000000000..7f4e403f22 --- /dev/null +++ b/core/java/android/net/InternalNetworkManagementException.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2021 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 android.net; + +import android.annotation.NonNull; +import android.os.Parcel; +import android.os.Parcelable; + +/** @hide */ +public final class InternalNetworkManagementException + extends RuntimeException implements Parcelable { + + /* @hide */ + public InternalNetworkManagementException(@NonNull final Throwable t) { + super(t); + } + + private InternalNetworkManagementException(@NonNull final Parcel source) { + super(source.readString()); + } + + @Override + public void writeToParcel(@NonNull Parcel dest, int flags) { + dest.writeString(getCause().getMessage()); + } + + @Override + public int describeContents() { + return 0; + } + + @NonNull + public static final Parcelable.Creator CREATOR = + new Parcelable.Creator() { + @Override + public InternalNetworkManagementException[] newArray(int size) { + return new InternalNetworkManagementException[size]; + } + + @Override + public InternalNetworkManagementException createFromParcel(@NonNull Parcel source) { + return new InternalNetworkManagementException(source); + } + }; +} diff --git a/core/java/android/net/InternalNetworkUpdateRequest.aidl b/core/java/android/net/InternalNetworkUpdateRequest.aidl new file mode 100644 index 0000000000..da00cb97af --- /dev/null +++ b/core/java/android/net/InternalNetworkUpdateRequest.aidl @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2021 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 android.net; + + parcelable InternalNetworkUpdateRequest; \ No newline at end of file diff --git a/core/java/android/net/InternalNetworkUpdateRequest.java b/core/java/android/net/InternalNetworkUpdateRequest.java new file mode 100644 index 0000000000..6f093835fb --- /dev/null +++ b/core/java/android/net/InternalNetworkUpdateRequest.java @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2021 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 android.net; + +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.os.Parcel; +import android.os.Parcelable; + +import java.util.Objects; + +/** @hide */ +public final class InternalNetworkUpdateRequest implements Parcelable { + @NonNull + private final StaticIpConfiguration mIpConfig; + @Nullable + private final NetworkCapabilities mNetworkCapabilities; + + @NonNull + public StaticIpConfiguration getIpConfig() { + return new StaticIpConfiguration(mIpConfig); + } + + @NonNull + public NetworkCapabilities getNetworkCapabilities() { + return mNetworkCapabilities == null + ? null : new NetworkCapabilities(mNetworkCapabilities); + } + + /** @hide */ + public InternalNetworkUpdateRequest(@NonNull final StaticIpConfiguration ipConfig, + @Nullable final NetworkCapabilities networkCapabilities) { + Objects.requireNonNull(ipConfig); + mIpConfig = new StaticIpConfiguration(ipConfig); + if (null == networkCapabilities) { + mNetworkCapabilities = null; + } else { + mNetworkCapabilities = new NetworkCapabilities(networkCapabilities); + } + } + + private InternalNetworkUpdateRequest(@NonNull final Parcel source) { + Objects.requireNonNull(source); + mIpConfig = StaticIpConfiguration.CREATOR.createFromParcel(source); + mNetworkCapabilities = NetworkCapabilities.CREATOR.createFromParcel(source); + } + + @Override + public String toString() { + return "InternalNetworkUpdateRequest{" + + "mIpConfig=" + mIpConfig + + ", mNetworkCapabilities=" + mNetworkCapabilities + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + InternalNetworkUpdateRequest that = (InternalNetworkUpdateRequest) o; + + return Objects.equals(that.getIpConfig(), mIpConfig) + && Objects.equals(that.getNetworkCapabilities(), mNetworkCapabilities); + } + + @Override + public int hashCode() { + return Objects.hash(mIpConfig, mNetworkCapabilities); + } + + @Override + public void writeToParcel(@NonNull Parcel dest, int flags) { + mIpConfig.writeToParcel(dest, flags); + mNetworkCapabilities.writeToParcel(dest, flags); + } + + @Override + public int describeContents() { + return 0; + } + + @NonNull + public static final Parcelable.Creator CREATOR = + new Parcelable.Creator() { + @Override + public InternalNetworkUpdateRequest[] newArray(int size) { + return new InternalNetworkUpdateRequest[size]; + } + + @Override + public InternalNetworkUpdateRequest createFromParcel(@NonNull Parcel source) { + return new InternalNetworkUpdateRequest(source); + } + }; +} diff --git a/framework-t/src/android/net/EthernetManager.java b/framework-t/src/android/net/EthernetManager.java index 7cd63ef9cc..ece54df966 100644 --- a/framework-t/src/android/net/EthernetManager.java +++ b/framework-t/src/android/net/EthernetManager.java @@ -16,7 +16,9 @@ package android.net; +import android.annotation.CallbackExecutor; import android.annotation.NonNull; +import android.annotation.Nullable; import android.annotation.RequiresPermission; import android.annotation.SystemApi; import android.annotation.SystemService; @@ -32,6 +34,7 @@ import com.android.internal.os.BackgroundThread; import java.util.ArrayList; import java.util.Objects; import java.util.concurrent.Executor; +import java.util.function.BiConsumer; /** * A class representing the IP configuration of the Ethernet network. @@ -315,4 +318,83 @@ public class EthernetManager { } return new TetheredInterfaceRequest(mService, cbInternal); } + + private static final class InternalNetworkManagementListener + extends IInternalNetworkManagementListener.Stub { + @NonNull + private final Executor mExecutor; + @NonNull + private final BiConsumer mListener; + + InternalNetworkManagementListener( + @NonNull final Executor executor, + @NonNull final BiConsumer listener) { + Objects.requireNonNull(executor, "Pass a non-null executor"); + Objects.requireNonNull(listener, "Pass a non-null listener"); + mExecutor = executor; + mListener = listener; + } + + @Override + public void onComplete( + @Nullable final Network network, + @Nullable final InternalNetworkManagementException e) { + mExecutor.execute(() -> mListener.accept(network, e)); + } + } + + private InternalNetworkManagementListener getInternalNetworkManagementListener( + @Nullable final Executor executor, + @Nullable final BiConsumer listener) { + if (null != listener) { + Objects.requireNonNull(executor, "Pass a non-null executor, or a null listener"); + } + final InternalNetworkManagementListener proxy; + if (null == listener) { + proxy = null; + } else { + proxy = new InternalNetworkManagementListener(executor, listener); + } + return proxy; + } + + private void updateConfiguration( + @NonNull String iface, + @NonNull InternalNetworkUpdateRequest request, + @Nullable @CallbackExecutor Executor executor, + @Nullable BiConsumer listener) { + final InternalNetworkManagementListener proxy = getInternalNetworkManagementListener( + executor, listener); + try { + mService.updateConfiguration(iface, request, proxy); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + private void connectNetwork( + @NonNull String iface, + @Nullable @CallbackExecutor Executor executor, + @Nullable BiConsumer listener) { + final InternalNetworkManagementListener proxy = getInternalNetworkManagementListener( + executor, listener); + try { + mService.connectNetwork(iface, proxy); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + private void disconnectNetwork( + @NonNull String iface, + @Nullable @CallbackExecutor Executor executor, + @Nullable BiConsumer listener) { + final InternalNetworkManagementListener proxy = getInternalNetworkManagementListener( + executor, listener); + try { + mService.disconnectNetwork(iface, proxy); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } } diff --git a/framework-t/src/android/net/IEthernetManager.aidl b/framework-t/src/android/net/IEthernetManager.aidl index e058e5a70c..e688bea1cf 100644 --- a/framework-t/src/android/net/IEthernetManager.aidl +++ b/framework-t/src/android/net/IEthernetManager.aidl @@ -18,6 +18,8 @@ package android.net; import android.net.IpConfiguration; import android.net.IEthernetServiceListener; +import android.net.IInternalNetworkManagementListener; +import android.net.InternalNetworkUpdateRequest; import android.net.ITetheredInterfaceCallback; /** @@ -36,4 +38,8 @@ interface IEthernetManager void setIncludeTestInterfaces(boolean include); void requestTetheredInterface(in ITetheredInterfaceCallback callback); void releaseTetheredInterface(in ITetheredInterfaceCallback callback); + void updateConfiguration(String iface, in InternalNetworkUpdateRequest request, + in IInternalNetworkManagementListener listener); + void connectNetwork(String iface, in IInternalNetworkManagementListener listener); + void disconnectNetwork(String iface, in IInternalNetworkManagementListener listener); }