Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Always-on VPN switch on programmatically android

Can Always-on VPN switch be on programmatically?

I have added the device admin permission. After that i have set always on in with device admin

mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    mDeviceAdminSample = new ComponentName(this, DeviceAdminReceiver.class);
    isAdminApp = mDPM.isAdminActive(mDeviceAdminSample);

    if (isAdminApp) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                mDPM.setAlwaysOnVpnPackage(mDeviceAdminSample,"", true);
            }
        } catch (PackageManager.NameNotFoundException namenotfoundexception) {
            namenotfoundexception.printStackTrace();
        } catch (Exception ex) {
        }
    }

but it's not enabling the always on.

i have added package name insted of

mDPM.setAlwaysOnVpnPackage(mDeviceAdminSample,"my.app.package.name", true);

but still not enabling the switch.

Then what this code is doing? How can i enable it programatically?

I want this to be like below image

Always-on VPN

like image 483
Wasi Sadman Avatar asked Jul 01 '21 15:07

Wasi Sadman


People also ask

How to use always-on VPN on Android devices?

To make Always-On VPN work you will need to activate Save account information. After clicking on Connect you should see Connected below the VPN server, indicating that the VPN connection is active. Tap the VPN connection again to disconnect. Now click on the More Options menu in the top right and click on Always-On VPN.

How to set up a VPN on Android devices?

Configuring the VPN network connection settings, including installing authentication certificates. Adding a list of apps that are allowed to use the VPN or a list of apps that can’t use the VPN. Android can start a VPN service when the device boots, and keep it running while the device or work profile is on.

How does a VPN app become active?

Before a VPN app can become active for the first time, the system displays a connection request dialog. The dialog prompts the person using the device to confirm that they trust the VPN and accept the request. The VPN settings screen (Settings > Network & Internet > VPN) shows the VPN apps where a person accepted connection requests.

What is the system UI for VPN on Android?

The system UI also makes the person using the device aware of an active VPN connection. Android shows the following UI components for VPN connections: Before a VPN app can become active for the first time, the system displays a connection request dialog.


1 Answers

According to docs, setAlwaysOnVpnPackage can only be used by the profile owner (usually the MDM client on work profile) or device owner (for fully managed devices):

Called by a device or profile owner to configure an always-on VPN connection through a specific application for the current user. This connection is automatically granted and persisted after a reboot.

As a personal profile user - I don't want my VPN to decide for itself when to connect (set always on programmatically will immediately connect the VPN, if implemented correctly).

As a work profile user (wearing the hat of an employee), it's not my decision, but my organization's (via the profile owner app).

So, all in all, this behavior makes sense.

Update:

Instead implementing MDM, which could take a lot of work, you can clone, build and debug Google's Test DPC app, which have everything you need to test toggling always-on VPN programmatically.

It also have million other things, which you don't need so be sure to ignore the rest :)

I haven't looked at their code, but I suggest searching for usages of setAlwaysOnVpnPackage function.

Google's Test DPC app:

  • Link to Play Store
  • Link to GitHub repo (to build & debug it yourself)
like image 148
Shlomi Katriel Avatar answered Oct 19 '22 03:10

Shlomi Katriel