1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.settings.vpn2; 17 18 import android.content.Context; 19 import android.net.VpnManager; 20 import android.os.RemoteException; 21 import android.provider.Settings; 22 import android.security.Credentials; 23 import android.security.LegacyVpnProfileStore; 24 25 import com.android.internal.net.LegacyVpnInfo; 26 import com.android.internal.net.VpnConfig; 27 28 /** 29 * Utility functions for vpn. 30 * 31 * LegacyVpnProfileStore methods should only be called in system user 32 */ 33 public class VpnUtils { 34 35 private static final String TAG = "VpnUtils"; 36 getLockdownVpn()37 public static String getLockdownVpn() { 38 final byte[] value = LegacyVpnProfileStore.get(Credentials.LOCKDOWN_VPN); 39 return value == null ? null : new String(value); 40 } 41 clearLockdownVpn(Context context)42 public static void clearLockdownVpn(Context context) { 43 LegacyVpnProfileStore.remove(Credentials.LOCKDOWN_VPN); 44 // Always notify VpnManager after keystore update 45 getVpnManager(context).updateLockdownVpn(); 46 } 47 setLockdownVpn(Context context, String lockdownKey)48 public static void setLockdownVpn(Context context, String lockdownKey) { 49 LegacyVpnProfileStore.put(Credentials.LOCKDOWN_VPN, lockdownKey.getBytes()); 50 // Always notify VpnManager after keystore update 51 getVpnManager(context).updateLockdownVpn(); 52 } 53 isVpnLockdown(String key)54 public static boolean isVpnLockdown(String key) { 55 return key.equals(getLockdownVpn()); 56 } 57 isAnyLockdownActive(Context context)58 public static boolean isAnyLockdownActive(Context context) { 59 final int userId = context.getUserId(); 60 if (getLockdownVpn() != null) { 61 return true; 62 } 63 return getVpnManager(context).getAlwaysOnVpnPackageForUser(userId) != null 64 && Settings.Secure.getIntForUser(context.getContentResolver(), 65 Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN, /* default */ 0, userId) != 0; 66 } 67 isVpnActive(Context context)68 public static boolean isVpnActive(Context context) throws RemoteException { 69 return getVpnManager(context).getVpnConfig(context.getUserId()) != null; 70 } 71 getConnectedPackage(VpnManager vpnManager, final int userId)72 public static String getConnectedPackage(VpnManager vpnManager, final int userId) { 73 final VpnConfig config = vpnManager.getVpnConfig(userId); 74 return config != null ? config.user : null; 75 } 76 getVpnManager(Context context)77 private static VpnManager getVpnManager(Context context) { 78 return context.getSystemService(VpnManager.class); 79 } 80 isAlwaysOnVpnSet(VpnManager vm, final int userId)81 public static boolean isAlwaysOnVpnSet(VpnManager vm, final int userId) { 82 return vm.getAlwaysOnVpnPackageForUser(userId) != null; 83 } 84 disconnectLegacyVpn(Context context)85 public static boolean disconnectLegacyVpn(Context context) { 86 int userId = context.getUserId(); 87 LegacyVpnInfo currentLegacyVpn = getVpnManager(context).getLegacyVpnInfo(userId); 88 if (currentLegacyVpn != null) { 89 clearLockdownVpn(context); 90 getVpnManager(context).prepareVpn(null, VpnConfig.LEGACY_VPN, userId); 91 return true; 92 } 93 return false; 94 } 95 } 96