From df2a66a4a1dd54d76a65ed516bc55ea3f521329d Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Fri, 22 Jul 2022 16:45:37 +0200 Subject: Refresh VPN list on resume() of VPN list fragment (closes #1508) --- .../de/blinkt/openvpn/core/ProfileManager.java | 99 ++++++++++++++-------- .../blinkt/openvpn/fragments/VPNProfileList.java | 1 + 2 files changed, 63 insertions(+), 37 deletions(-) diff --git a/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java b/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java index 06822817..1185a697 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java +++ b/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java @@ -241,6 +241,29 @@ public class ProfileManager { profiles.put(profile.getUUID().toString(), profile); } + /** + * Checks if a profile has been added deleted since last loading and will update its + * profiles + * @param context + */ + public void refreshVPNList(Context context) + { + SharedPreferences listpref = Preferences.getSharedPreferencesMulti(PREFS_NAME, context); + Set vlist = listpref.getStringSet("vpnlist", null); + if (vlist == null) + return; + + for (String vpnentry : vlist) { + if (!profiles.containsKey(vpnentry)) + loadVpnEntry(context, vpnentry); + } + for (String profileuuid:profiles.keySet()) + { + if (!vlist.contains(profileuuid)) + profiles.remove(profileuuid); + } + } + private void loadVPNList(Context context) { profiles = new HashMap<>(); SharedPreferences listpref = Preferences.getSharedPreferencesMulti(PREFS_NAME, context); @@ -252,44 +275,46 @@ public class ProfileManager { vlist.add(TEMPORARY_PROFILE_FILENAME); for (String vpnentry : vlist) { - ObjectInputStream vpnfile = null; - try { - FileInputStream vpInput; - File encryptedPath = context.getFileStreamPath(vpnentry + ".cp"); - File encryptedPathOld = context.getFileStreamPath(vpnentry + ".cpold"); - - if (encryptedPath.exists()) { - vpInput = ProfileEncryption.getEncryptedVpInput(context, encryptedPath); - } else if (encryptedPathOld.exists()) { - vpInput = ProfileEncryption.getEncryptedVpInput(context, encryptedPathOld); - } else { - vpInput = context.openFileInput(vpnentry + ".vp"); - } - vpnfile = new ObjectInputStream(vpInput); - VpnProfile vp = ((VpnProfile) vpnfile.readObject()); - - // Sanity check - if (vp == null || vp.mName == null || vp.getUUID() == null) - continue; - - vp.upgradeProfile(); - if (vpnentry.equals(TEMPORARY_PROFILE_FILENAME)) { - tmpprofile = vp; - } else { - profiles.put(vp.getUUID().toString(), vp); - } - + loadVpnEntry(context, vpnentry); + } + } - } catch (IOException | ClassNotFoundException | GeneralSecurityException e) { - if (!vpnentry.equals(TEMPORARY_PROFILE_FILENAME)) - VpnStatus.logException("Loading VPN List", e); - } finally { - if (vpnfile != null) { - try { - vpnfile.close(); - } catch (IOException e) { - e.printStackTrace(); - } + private void loadVpnEntry(Context context, String vpnentry) { + ObjectInputStream vpnfile = null; + try { + FileInputStream vpInput; + File encryptedPath = context.getFileStreamPath(vpnentry + ".cp"); + File encryptedPathOld = context.getFileStreamPath(vpnentry + ".cpold"); + + if (encryptedPath.exists()) { + vpInput = ProfileEncryption.getEncryptedVpInput(context, encryptedPath); + } else if (encryptedPathOld.exists()) { + vpInput = ProfileEncryption.getEncryptedVpInput(context, encryptedPathOld); + } else { + vpInput = context.openFileInput(vpnentry + ".vp"); + } + vpnfile = new ObjectInputStream(vpInput); + VpnProfile vp = ((VpnProfile) vpnfile.readObject()); + + // Sanity check + if (vp == null || vp.mName == null || vp.getUUID() == null) + return; + + vp.upgradeProfile(); + if (vpnentry.equals(TEMPORARY_PROFILE_FILENAME)) { + tmpprofile = vp; + } else { + profiles.put(vp.getUUID().toString(), vp); + } + } catch (IOException | ClassNotFoundException | GeneralSecurityException e) { + if (!vpnentry.equals(TEMPORARY_PROFILE_FILENAME)) + VpnStatus.logException("Loading VPN List", e); + } finally { + if (vpnfile != null) { + try { + vpnfile.close(); + } catch (IOException e) { + e.printStackTrace(); } } } diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/VPNProfileList.java b/main/src/ui/java/de/blinkt/openvpn/fragments/VPNProfileList.java index cf48cc48..c7ee5df1 100644 --- a/main/src/ui/java/de/blinkt/openvpn/fragments/VPNProfileList.java +++ b/main/src/ui/java/de/blinkt/openvpn/fragments/VPNProfileList.java @@ -292,6 +292,7 @@ public class VPNProfileList extends ListFragment implements OnClickListener, Vpn private void populateVpnList() { boolean sortByLRU = Preferences.getDefaultSharedPreferences(requireActivity()).getBoolean(PREF_SORT_BY_LRU, false); + getPM().refreshVPNList(requireContext()); Collection allvpn = getPM().getProfiles(); TreeSet sortedset; if (sortByLRU) -- cgit v1.2.3