2 * Copyright (C) 2025 Tobias Brunner
3 * Copyright (C) 2023 Relution GmbH
5 * Copyright (C) secunet Security Networks AG
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 package org.strongswan.android.data;
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 import android.database.SQLException;
24 import org.strongswan.android.logic.StrongSwanApplication;
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.List;
31 import java.util.UUID;
33 public class VpnProfileManagedDataSource implements VpnProfileDataSource
35 private static final String NAME_MANAGED_VPN_PROFILES = "org.strongswan.android.data.VpnProfileManagedDataSource.preferences";
37 private final ManagedConfigurationService mManagedConfigurationService;
38 private final SharedPreferences mSharedPreferences;
40 public VpnProfileManagedDataSource(final Context context)
42 this.mManagedConfigurationService = StrongSwanApplication.getInstance().getManagedConfigurationService();
43 this.mSharedPreferences = context.getSharedPreferences(NAME_MANAGED_VPN_PROFILES, Context.MODE_PRIVATE);
47 public VpnProfileDataSource open() throws SQLException
55 /* remove passwords that are no longer referenced by a VPN profile */
56 final Set<String> actualKeys = mManagedConfigurationService.getManagedProfiles().keySet();
58 final Set<String> storedKeys = new HashSet<>(mSharedPreferences.getAll().keySet());
59 storedKeys.removeAll(actualKeys);
61 final SharedPreferences.Editor editor = mSharedPreferences.edit();
62 for (String key : storedKeys)
71 public VpnProfile insertProfile(VpnProfile profile)
77 public boolean updateVpnProfile(VpnProfile profile)
79 final VpnProfile managedProfile = mManagedConfigurationService.getManagedProfiles().get(profile.getUUID().toString());
80 if (managedProfile == null)
85 final SharedPreferences.Editor editor = mSharedPreferences.edit();
86 editor.putString(profile.getUUID().toString(), profile.getPassword());
87 return editor.commit();
91 public boolean deleteVpnProfile(VpnProfile profile)
97 * Clone and prepare the given managed profile before handing it out.
98 * @param managedProfile profile to prepare
100 private VpnProfile prepareVpnProfile(VpnProfile managedProfile)
102 final String password = mSharedPreferences.getString(managedProfile.getUUID().toString(), managedProfile.getPassword());
103 final VpnProfile vpnProfile = managedProfile.clone();
104 vpnProfile.setPassword(password);
105 vpnProfile.setDataSource(this);
110 public VpnProfile getVpnProfile(UUID uuid)
112 final VpnProfile managedProfile = mManagedConfigurationService.getManagedProfiles().get(uuid.toString());
113 if (managedProfile != null)
115 return prepareVpnProfile(managedProfile);
121 public List<VpnProfile> getAllVpnProfiles()
123 final Map<String, ManagedVpnProfile> managedVpnProfiles = mManagedConfigurationService.getManagedProfiles();
124 final List<VpnProfile> vpnProfiles = new ArrayList<>();
125 for (final VpnProfile managedProfile : managedVpnProfiles.values())
127 vpnProfiles.add(prepareVpnProfile(managedProfile));