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

Commit 96ed3f69 authored by Junyu Lai's avatar Junyu Lai Committed by Automerger Merge Worker
Browse files

Merge "[FUI04] Refactor VpnInfo" am: 561626b2 am: 0824b59f am: 9e10a5a1

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1537211

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Icfa9918e41fc3f952a23956108df4db449741364
parents ec5c4c56 9e10a5a1
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ import android.net.NetworkRequest;
import android.net.NetworkState;
import android.net.ProxyInfo;
import android.net.UidRange;
import android.net.VpnInfo;
import android.net.QosSocketInfo;
import android.os.Bundle;
import android.os.IBinder;
+2 −2
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ import android.net.NetworkState;
import android.net.NetworkStats;
import android.net.NetworkStatsHistory;
import android.net.NetworkTemplate;
import android.net.VpnInfo;
import android.net.UnderlyingNetworkInfo;
import android.net.netstats.provider.INetworkStatsProvider;
import android.net.netstats.provider.INetworkStatsProviderCallback;
import android.os.IBinder;
@@ -70,7 +70,7 @@ interface INetworkStatsService {
         in Network[] defaultNetworks,
         in NetworkState[] networkStates,
         in String activeIface,
         in VpnInfo[] vpnInfos);
         in UnderlyingNetworkInfo[] underlyingNetworkInfos);
    /** Force update of statistics. */
    @UnsupportedAppUsage
    void forceUpdate();
+1 −1
Original line number Diff line number Diff line
@@ -16,4 +16,4 @@

package android.net;

parcelable VpnInfo;
parcelable UnderlyingNetworkInfo;
+110 −0
Original line number Diff line number Diff line
@@ -17,43 +17,51 @@
package android.net;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Parcel;
import android.os.Parcelable;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * A lightweight container used to carry information of the ongoing VPN.
 * Internal use only.
 * A lightweight container used to carry information on the networks that underly a given
 * virtual network.
 *
 * @hide
 */
public class VpnInfo implements Parcelable {
public final class UnderlyingNetworkInfo implements Parcelable {
    /** The owner of this network. */
    public final int ownerUid;
    @Nullable
    public final String vpnIface;
    @Nullable
    public final String[] underlyingIfaces;
    /** The interface name of this network. */
    @NonNull
    public final String iface;
    /** The names of the interfaces underlying this network. */
    @NonNull
    public final List<String> underlyingIfaces;

    public VpnInfo(int ownerUid, @Nullable String vpnIface, @Nullable String[] underlyingIfaces) {
    public UnderlyingNetworkInfo(int ownerUid, @NonNull String iface,
            @NonNull List<String> underlyingIfaces) {
        Objects.requireNonNull(iface);
        Objects.requireNonNull(underlyingIfaces);
        this.ownerUid = ownerUid;
        this.vpnIface = vpnIface;
        this.iface = iface;
        this.underlyingIfaces = underlyingIfaces;
    }

    private VpnInfo(@NonNull Parcel in) {
    private UnderlyingNetworkInfo(@NonNull Parcel in) {
        this.ownerUid = in.readInt();
        this.vpnIface = in.readString();
        this.underlyingIfaces = in.createStringArray();
        this.iface = in.readString();
        this.underlyingIfaces = new ArrayList<>();
        in.readList(this.underlyingIfaces, null /*classLoader*/);
    }

    @Override
    public String toString() {
        return "VpnInfo{"
        return "UnderlyingNetworkInfo{"
                + "ownerUid=" + ownerUid
                + ", vpnIface='" + vpnIface + '\''
                + ", underlyingIfaces='" + Arrays.toString(underlyingIfaces) + '\''
                + ", iface='" + iface + '\''
                + ", underlyingIfaces='" + underlyingIfaces.toString() + '\''
                + '}';
    }

@@ -65,22 +73,38 @@ public class VpnInfo implements Parcelable {
    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeInt(ownerUid);
        dest.writeString(vpnIface);
        dest.writeStringArray(underlyingIfaces);
        dest.writeString(iface);
        dest.writeList(underlyingIfaces);
    }

    @NonNull
    public static final Parcelable.Creator<VpnInfo> CREATOR = new Parcelable.Creator<VpnInfo>() {
    public static final Parcelable.Creator<UnderlyingNetworkInfo> CREATOR =
            new Parcelable.Creator<UnderlyingNetworkInfo>() {
        @NonNull
        @Override
        public VpnInfo createFromParcel(@NonNull Parcel in) {
            return new VpnInfo(in);
        public UnderlyingNetworkInfo createFromParcel(@NonNull Parcel in) {
            return new UnderlyingNetworkInfo(in);
        }

        @NonNull
        @Override
        public VpnInfo[] newArray(int size) {
            return new VpnInfo[size];
        public UnderlyingNetworkInfo[] newArray(int size) {
            return new UnderlyingNetworkInfo[size];
        }
    };

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof UnderlyingNetworkInfo)) return false;
        final UnderlyingNetworkInfo that = (UnderlyingNetworkInfo) o;
        return ownerUid == that.ownerUid
                && Objects.equals(iface, that.iface)
                && Objects.equals(underlyingIfaces, that.underlyingIfaces);
    }

    @Override
    public int hashCode() {
        return Objects.hash(ownerUid, iface, underlyingIfaces);
    }
}
+15 −14
Original line number Diff line number Diff line
@@ -132,8 +132,8 @@ import android.net.SocketKeepalive;
import android.net.TetheringManager;
import android.net.UidRange;
import android.net.UidRangeParcel;
import android.net.UnderlyingNetworkInfo;
import android.net.Uri;
import android.net.VpnInfo;
import android.net.VpnManager;
import android.net.VpnService;
import android.net.metrics.INetdEventListener;
@@ -4854,28 +4854,28 @@ public class ConnectivityService extends IConnectivityManager.Stub
     *
     * <p>Must be called on the handler thread.
     */
    private VpnInfo[] getAllVpnInfo() {
    private UnderlyingNetworkInfo[] getAllVpnInfo() {
        ensureRunningOnConnectivityServiceThread();
        synchronized (mVpns) {
            if (mLockdownEnabled) {
                return new VpnInfo[0];
                return new UnderlyingNetworkInfo[0];
            }
        }
        List<VpnInfo> infoList = new ArrayList<>();
        List<UnderlyingNetworkInfo> infoList = new ArrayList<>();
        for (NetworkAgentInfo nai : mNetworkAgentInfos) {
            VpnInfo info = createVpnInfo(nai);
            UnderlyingNetworkInfo info = createVpnInfo(nai);
            if (info != null) {
                infoList.add(info);
            }
        }
        return infoList.toArray(new VpnInfo[infoList.size()]);
        return infoList.toArray(new UnderlyingNetworkInfo[infoList.size()]);
    }

    /**
     * @return VPN information for accounting, or null if we can't retrieve all required
     *         information, e.g underlying ifaces.
     */
    private VpnInfo createVpnInfo(NetworkAgentInfo nai) {
    private UnderlyingNetworkInfo createVpnInfo(NetworkAgentInfo nai) {
        if (!nai.isVPN()) return null;

        Network[] underlyingNetworks = nai.declaredUnderlyingNetworks;
@@ -4907,11 +4907,11 @@ public class ConnectivityService extends IConnectivityManager.Stub
        // Must be non-null or NetworkStatsService will crash.
        // Cannot happen in production code because Vpn only registers the NetworkAgent after the
        // tun or ipsec interface is created.
        // TODO: Remove this check.
        if (nai.linkProperties.getInterfaceName() == null) return null;

        return new VpnInfo(nai.networkCapabilities.getOwnerUid(),
                nai.linkProperties.getInterfaceName(),
                interfaces.toArray(new String[0]));
        return new UnderlyingNetworkInfo(nai.networkCapabilities.getOwnerUid(),
                nai.linkProperties.getInterfaceName(), interfaces);
    }

    /**
@@ -8001,10 +8001,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
            activeIface = activeLinkProperties.getInterfaceName();
        }

        final VpnInfo[] vpnInfos = getAllVpnInfo();
        final UnderlyingNetworkInfo[] underlyingNetworkInfos = getAllVpnInfo();
        try {
            mStatsService.forceUpdateIfaces(
                    getDefaultNetworks(), getAllNetworkState(), activeIface, vpnInfos);
            mStatsService.forceUpdateIfaces(getDefaultNetworks(), getAllNetworkState(), activeIface,
                    underlyingNetworkInfos);
        } catch (Exception ignored) {
        }
    }
@@ -8264,6 +8264,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
        return getVpnIfOwner(mDeps.getCallingUid());
    }

    // TODO: stop calling into Vpn.java and get this information from data in this class.
    @GuardedBy("mVpns")
    private Vpn getVpnIfOwner(int uid) {
        final int user = UserHandle.getUserId(uid);
@@ -8272,7 +8273,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
        if (vpn == null) {
            return null;
        } else {
            final VpnInfo info = vpn.getVpnInfo();
            final UnderlyingNetworkInfo info = vpn.getUnderlyingNetworkInfo();
            return (info == null || info.ownerUid != uid) ? null : vpn;
        }
    }
Loading