import static android.system.OsConstants.AF_INET;
import static android.system.OsConstants.AF_INET6;
+import android.annotation.SystemApi;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.Service;
}
/**
+ * Version of {@link #prepare(Context)} which does not require user consent.
+ *
+ * <p>Requires {@link android.Manifest.permission#CONTROL_VPN} and should generally not be
+ * used. Only acceptable in situations where user consent has been obtained through other means.
+ *
+ * <p>Once this is run, future preparations may be done with the standard prepare method as this
+ * will authorize the package to prepare the VPN without consent in the future.
+ *
+ * @hide
+ */
+ @SystemApi
+ public static void prepareAndAuthorize(Context context) {
+ IConnectivityManager cm = getService();
+ String packageName = context.getPackageName();
+ try {
+ // Only prepare if we're not already prepared.
+ if (!cm.prepareVpn(packageName, null)) {
+ cm.prepareVpn(null, packageName);
+ }
+ cm.setVpnPackageAuthorization(true);
+ } catch (RemoteException e) {
+ // ignore
+ }
+ }
+
+ /**
* Protect a socket from VPN connections. After protecting, data sent
* through this socket will go directly to the underlying network,
* so its traffic will not be forwarded through the VPN.
}
/**
- * Prepare for a VPN application. This method is used by VpnDialogs
- * and not available in ConnectivityManager. Permissions are checked
- * in Vpn class.
+ * Prepare for a VPN application. This method is used by system-privileged apps.
+ * Permissions are checked in Vpn class.
* @hide
*/
@Override
/**
* Set whether the current VPN package has the ability to launch VPNs without
- * user intervention. This method is used by system UIs and not available
- * in ConnectivityManager. Permissions are checked in Vpn class.
+ * user intervention. This method is used by system-privileged apps.
+ * Permissions are checked in Vpn class.
* @hide
*/
@Override