Define and implement getProvisionedVpnProfileState()

CTS-Coverage-Bug: 225010642
Bug: 225010642
Test: CtsNetTestCases:Ikev2VpnTest
Change-Id: I67470903bba2f783e316be47a50c438e049ca856
This commit is contained in:
lucaslin
2022-03-18 02:34:44 +08:00
committed by Lucas Lin
parent f306a53e48
commit 0ae29f0cd5
5 changed files with 77 additions and 0 deletions

View File

@@ -25507,6 +25507,7 @@ package android.net {
public class VpnManager {
method public void deleteProvisionedVpnProfile();
method @Nullable public android.net.VpnProfileState getProvisionedVpnProfileState();
method @Nullable public android.content.Intent provisionVpnProfile(@NonNull android.net.PlatformVpnProfile);
method @Deprecated public void startProvisionedVpnProfile();
method @NonNull public String startProvisionedVpnProfileSession();

View File

@@ -17,6 +17,7 @@
package android.net;
import android.net.Network;
import android.net.VpnProfileState;
import com.android.internal.net.LegacyVpnInfo;
import com.android.internal.net.VpnConfig;
@@ -40,6 +41,7 @@ interface IVpnManager {
void deleteVpnProfile(String packageName);
String startVpnProfile(String packageName);
void stopVpnProfile(String packageName);
VpnProfileState getProvisionedVpnProfileState(String packageName);
/** Always-on VPN APIs */
boolean isAlwaysOnVpnPackageSupported(int userId, String packageName);

View File

@@ -419,6 +419,21 @@ public class VpnManager {
}
}
/**
* Retrieve the VpnProfileState for the profile provisioned by the calling package.
*
* @return the VpnProfileState with current information, or null if there was no profile
* provisioned by the calling package.
*/
@Nullable
public VpnProfileState getProvisionedVpnProfileState() {
try {
return mService.getProvisionedVpnProfileState(mContext.getOpPackageName());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Resets all VPN settings back to factory defaults.
* @hide

View File

@@ -37,6 +37,7 @@ import android.net.NetworkStack;
import android.net.UnderlyingNetworkInfo;
import android.net.Uri;
import android.net.VpnManager;
import android.net.VpnProfileState;
import android.net.VpnService;
import android.net.util.NetdService;
import android.os.Binder;
@@ -373,6 +374,24 @@ public class VpnManagerService extends IVpnManager.Stub {
}
}
/**
* Retrieve the VpnProfileState for the profile provisioned by the given package.
*
* @return the VpnProfileState with current information, or null if there was no profile
* provisioned by the given package.
* @hide
*/
@Override
@Nullable
public VpnProfileState getProvisionedVpnProfileState(@NonNull String packageName) {
final int callingUid = Binder.getCallingUid();
verifyCallingUidAndPackage(packageName, callingUid);
final int user = UserHandle.getUserId(callingUid);
synchronized (mVpns) {
return mVpns.get(user).getProvisionedVpnProfileState(packageName);
}
}
/**
* Start legacy VPN, controlling native daemons as needed. Creates a
* secondary thread to perform connection work, returning quickly.

View File

@@ -75,6 +75,7 @@ import android.net.RouteInfo;
import android.net.UidRangeParcel;
import android.net.UnderlyingNetworkInfo;
import android.net.VpnManager;
import android.net.VpnProfileState;
import android.net.VpnService;
import android.net.VpnTransportInfo;
import android.net.ipsec.ike.ChildSessionCallback;
@@ -3438,6 +3439,45 @@ public class Vpn {
}
}
private @VpnProfileState.State int getStateFromLegacyState(int legacyState) {
switch (legacyState) {
case LegacyVpnInfo.STATE_CONNECTING:
return VpnProfileState.STATE_CONNECTING;
case LegacyVpnInfo.STATE_CONNECTED:
return VpnProfileState.STATE_CONNECTED;
case LegacyVpnInfo.STATE_DISCONNECTED:
return VpnProfileState.STATE_DISCONNECTED;
case LegacyVpnInfo.STATE_FAILED:
return VpnProfileState.STATE_FAILED;
default:
Log.wtf(TAG, "Unhandled state " + legacyState
+ ", treat it as STATE_DISCONNECTED");
return VpnProfileState.STATE_DISCONNECTED;
}
}
private VpnProfileState makeVpnProfileState() {
// TODO: mSessionKey will be moved to Ikev2VpnRunner once aosp/2007077 is merged, so after
// merging aosp/2007077, here should check Ikev2VpnRunner is null or not. Session key will
// be null if Ikev2VpnRunner is null.
return new VpnProfileState(getStateFromLegacyState(mLegacyState), mSessionKey, mAlwaysOn,
mLockdown);
}
/**
* Retrieve the VpnProfileState for the profile provisioned by the given package.
*
* @return the VpnProfileState with current information, or null if there was no profile
* provisioned by the given package.
*/
@Nullable
public synchronized VpnProfileState getProvisionedVpnProfileState(
@NonNull String packageName) {
requireNonNull(packageName, "No package name provided");
enforceNotRestrictedUser();
return isCurrentIkev2VpnLocked(packageName) ? makeVpnProfileState() : null;
}
/**
* Proxy to allow testing
*