Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 48a10628 authored by Cody Kesting's avatar Cody Kesting Committed by Gerrit Code Review
Browse files

Merge changes I61dd789f,I57c73264

* changes:
  Define add and remove for VCN policy listeners.
  Define VcnUnderlyingNetworkPolicy.
parents 96a8a5b9 80cde857
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.net.vcn;

import android.net.vcn.IVcnUnderlyingNetworkPolicyListener;
import android.net.vcn.VcnConfig;
import android.os.ParcelUuid;

@@ -25,4 +26,7 @@ import android.os.ParcelUuid;
interface IVcnManagementService {
    void setVcnConfig(in ParcelUuid subscriptionGroup, in VcnConfig config, in String opPkgName);
    void clearVcnConfig(in ParcelUuid subscriptionGroup);

    void addVcnUnderlyingNetworkPolicyListener(in IVcnUnderlyingNetworkPolicyListener listener);
    void removeVcnUnderlyingNetworkPolicyListener(in IVcnUnderlyingNetworkPolicyListener listener);
}
+22 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.vcn;

/** @hide */
interface IVcnUnderlyingNetworkPolicyListener {
    void onPolicyChanged();
}
 No newline at end of file
+107 −0
Original line number Diff line number Diff line
@@ -25,7 +25,12 @@ import android.os.ParcelUuid;
import android.os.RemoteException;
import android.os.ServiceSpecificException;

import com.android.internal.annotations.VisibleForTesting;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;

/**
 * VcnManager publishes APIs for applications to configure and manage Virtual Carrier Networks.
@@ -60,6 +65,11 @@ import java.io.IOException;
public final class VcnManager {
    @NonNull private static final String TAG = VcnManager.class.getSimpleName();

    @VisibleForTesting
    public static final Map<
                    VcnUnderlyingNetworkPolicyListener, VcnUnderlyingNetworkPolicyListenerBinder>
            REGISTERED_POLICY_LISTENERS = new ConcurrentHashMap<>();

    @NonNull private final Context mContext;
    @NonNull private final IVcnManagementService mService;

@@ -136,4 +146,101 @@ public final class VcnManager {
            throw e.rethrowFromSystemServer();
        }
    }

    // TODO: make VcnUnderlyingNetworkPolicyListener @SystemApi
    /**
     * VcnUnderlyingNetworkPolicyListener is the interface through which internal system components
     * can register to receive updates for VCN-underlying Network policies from the System Server.
     *
     * @hide
     */
    public interface VcnUnderlyingNetworkPolicyListener {
        /**
         * Notifies the implementation that the VCN's underlying Network policy has changed.
         *
         * <p>After receiving this callback, implementations MUST poll VcnManager for the updated
         * VcnUnderlyingNetworkPolicy via VcnManager#getUnderlyingNetworkPolicy.
         */
        void onPolicyChanged();
    }

    /**
     * Add a listener for VCN-underlying network policy updates.
     *
     * @param executor the Executor that will be used for invoking all calls to the specified
     *     Listener
     * @param listener the VcnUnderlyingNetworkPolicyListener to be added
     * @throws SecurityException if the caller does not have permission NETWORK_FACTORY
     * @throws IllegalArgumentException if the specified VcnUnderlyingNetworkPolicyListener is
     *     already registered
     * @hide
     */
    @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
    public void addVcnUnderlyingNetworkPolicyListener(
            @NonNull Executor executor, @NonNull VcnUnderlyingNetworkPolicyListener listener) {
        requireNonNull(executor, "executor must not be null");
        requireNonNull(listener, "listener must not be null");

        VcnUnderlyingNetworkPolicyListenerBinder binder =
                new VcnUnderlyingNetworkPolicyListenerBinder(executor, listener);
        if (REGISTERED_POLICY_LISTENERS.putIfAbsent(listener, binder) != null) {
            throw new IllegalArgumentException(
                    "Attempting to add a listener that is already in use");
        }

        try {
            mService.addVcnUnderlyingNetworkPolicyListener(binder);
        } catch (RemoteException e) {
            REGISTERED_POLICY_LISTENERS.remove(listener);
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Remove the specified VcnUnderlyingNetworkPolicyListener from VcnManager.
     *
     * <p>If the specified listener is not currently registered, this is a no-op.
     *
     * @param listener the VcnUnderlyingNetworkPolicyListener that will be removed
     * @hide
     */
    public void removeVcnUnderlyingNetworkPolicyListener(
            @NonNull VcnUnderlyingNetworkPolicyListener listener) {
        requireNonNull(listener, "listener must not be null");

        VcnUnderlyingNetworkPolicyListenerBinder binder =
                REGISTERED_POLICY_LISTENERS.remove(listener);
        if (binder == null) {
            return;
        }

        try {
            mService.removeVcnUnderlyingNetworkPolicyListener(binder);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Binder wrapper for added VcnUnderlyingNetworkPolicyListeners to receive signals from System
     * Server.
     *
     * @hide
     */
    private static class VcnUnderlyingNetworkPolicyListenerBinder
            extends IVcnUnderlyingNetworkPolicyListener.Stub {
        @NonNull private final Executor mExecutor;
        @NonNull private final VcnUnderlyingNetworkPolicyListener mListener;

        private VcnUnderlyingNetworkPolicyListenerBinder(
                Executor executor, VcnUnderlyingNetworkPolicyListener listener) {
            mExecutor = executor;
            mListener = listener;
        }

        @Override
        public void onPolicyChanged() {
            mExecutor.execute(() -> mListener.onPolicyChanged());
        }
    }
}
+20 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.vcn;

/** @hide */
parcelable VcnUnderlyingNetworkPolicy;
+110 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.vcn;

import android.annotation.NonNull;
import android.net.NetworkCapabilities;
import android.os.Parcel;
import android.os.Parcelable;

import java.util.Objects;

/**
 * VcnUnderlyingNetworkPolicy represents the Network policy for a VCN-managed Network.
 *
 * <p>Transports that are bringing up networks capable of acting as a VCN's underlying network
 * should query for policy state upon major capability changes (e.g. changing of TRUSTED bit), and
 * when prompted by VcnManagementService via VcnUnderlyingNetworkPolicyListener.
 *
 * @hide
 */
public final class VcnUnderlyingNetworkPolicy implements Parcelable {
    private final boolean mIsTearDownRequested;
    private final NetworkCapabilities mMergedNetworkCapabilities;

    /**
     * Constructs a VcnUnderlyingNetworkPolicy with the specified parameters.
     *
     * @hide
     */
    public VcnUnderlyingNetworkPolicy(
            boolean isTearDownRequested, @NonNull NetworkCapabilities mergedNetworkCapabilities) {
        Objects.requireNonNull(
                mergedNetworkCapabilities, "mergedNetworkCapabilities must be nonnull");

        mIsTearDownRequested = isTearDownRequested;
        mMergedNetworkCapabilities = mergedNetworkCapabilities;
    }

    /**
     * Returns whether this Carrier VCN policy policy indicates that the underlying Network should
     * be torn down.
     */
    public boolean isTeardownRequested() {
        return mIsTearDownRequested;
    }

    /**
     * Returns the NetworkCapabilities with Carrier VCN policy bits merged into the provided
     * capabilities.
     */
    @NonNull
    public NetworkCapabilities getMergedNetworkCapabilities() {
        return mMergedNetworkCapabilities;
    }

    @Override
    public int hashCode() {
        return Objects.hash(mIsTearDownRequested, mMergedNetworkCapabilities);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof VcnUnderlyingNetworkPolicy)) return false;
        final VcnUnderlyingNetworkPolicy that = (VcnUnderlyingNetworkPolicy) o;

        return mIsTearDownRequested == that.mIsTearDownRequested
                && mMergedNetworkCapabilities.equals(that.mMergedNetworkCapabilities);
    }

    /** {@inheritDoc} */
    @Override
    public int describeContents() {
        return 0;
    }

    /** {@inheritDoc} */
    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeBoolean(mIsTearDownRequested);
        dest.writeParcelable(mMergedNetworkCapabilities, flags);
    }

    /** Implement the Parcelable interface */
    public static final @NonNull Creator<VcnUnderlyingNetworkPolicy> CREATOR =
            new Creator<VcnUnderlyingNetworkPolicy>() {
                public VcnUnderlyingNetworkPolicy createFromParcel(Parcel in) {
                    return new VcnUnderlyingNetworkPolicy(
                            in.readBoolean(), in.readParcelable(null));
                }

                public VcnUnderlyingNetworkPolicy[] newArray(int size) {
                    return new VcnUnderlyingNetworkPolicy[size];
                }
            };
}
Loading