2 * Copyright (C) 2023 Relution GmbH
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 package org.strongswan.android.data;
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.database.SQLException;
23 import org.strongswan.android.logic.StrongSwanApplication;
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.List;
30 import java.util.UUID;
32 public class VpnProfileManagedDataSource implements VpnProfileDataSource
34 private static final String NAME_MANAGED_VPN_PROFILES = "org.strongswan.android.data.VpnProfileManagedDataSource.preferences";
36 private final ManagedConfigurationService mManagedConfigurationService;
37 private final SharedPreferences mSharedPreferences;
39 public VpnProfileManagedDataSource(final Context context)
41 this.mManagedConfigurationService = StrongSwanApplication.getInstance().getManagedConfigurationService();
42 this.mSharedPreferences = context.getSharedPreferences(NAME_MANAGED_VPN_PROFILES, Context.MODE_PRIVATE);
46 public VpnProfileDataSource open() throws SQLException
54 /* remove passwords that are no longer referenced by a VPN profile */
55 final Set<String> actualKeys = mManagedConfigurationService.getManagedProfiles().keySet();
57 final Set<String> storedKeys = new HashSet<>(mSharedPreferences.getAll().keySet());
58 storedKeys.removeAll(actualKeys);
60 final SharedPreferences.Editor editor = mSharedPreferences.edit();
61 for (String key : storedKeys)
70 public VpnProfile insertProfile(VpnProfile profile)
76 public boolean updateVpnProfile(VpnProfile profile)
78 final VpnProfile existingProfile = getVpnProfile(profile.getUUID());
79 if (existingProfile == null)
84 final String password = profile.getPassword();
85 existingProfile.setPassword(password);
87 final SharedPreferences.Editor editor = mSharedPreferences.edit();
88 editor.putString(profile.getUUID().toString(), password);
89 return editor.commit();
93 public boolean deleteVpnProfile(VpnProfile profile)
99 public VpnProfile getVpnProfile(UUID uuid)
101 return mManagedConfigurationService.getManagedProfiles().get(uuid.toString());
105 public List<VpnProfile> getAllVpnProfiles()
107 final Map<String, ManagedVpnProfile> managedVpnProfiles = mManagedConfigurationService.getManagedProfiles();
108 final List<VpnProfile> vpnProfiles = new ArrayList<>();
109 for (final VpnProfile vpnProfile : managedVpnProfiles.values())
111 final String password = mSharedPreferences.getString(vpnProfile.getUUID().toString(), vpnProfile.getPassword());
112 vpnProfile.setPassword(password);
113 vpnProfile.setDataSource(this);
114 vpnProfiles.add(vpnProfile);