]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
android: Disable copy/delete for read-only profiles
authorMarkus Pfeiffer <markus.pfeiffer@relution.io>
Tue, 21 Nov 2023 14:37:21 +0000 (15:37 +0100)
committerTobias Brunner <tobias@strongswan.org>
Wed, 21 Feb 2024 11:24:53 +0000 (12:24 +0100)
If a profile is marked as read-only, do not allow users to copy or
delete the profile.

src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnProfileListFragment.java

index 7c3572325d04fc696ae1b1a3955ebf48b431ca08..c56962d72bf021f240a7ed4a4fe5c3b8969df970 100644 (file)
@@ -1,4 +1,5 @@
 /*
+ * Copyright (C) 2023 Relution GmbH
  * Copyright (C) 2012-2019 Tobias Brunner
  * Copyright (C) 2012 Giuliano Grassi
  * Copyright (C) 2012 Ralf Sager
@@ -50,6 +51,7 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Objects;
+import java.util.Set;
 import java.util.UUID;
 
 import androidx.fragment.app.Fragment;
@@ -64,7 +66,7 @@ public class VpnProfileListFragment extends Fragment
        private VpnProfileAdapter mListAdapter;
        private ListView mListView;
        private OnVpnProfileSelectedListener mListener;
-       private HashSet<Integer> mSelected;
+       private Set<Integer> mSelected;
        private boolean mReadOnly;
 
        private final BroadcastReceiver mProfilesChanged = new BroadcastReceiver()
@@ -237,18 +239,27 @@ public class VpnProfileListFragment extends Fragment
        {
                private MenuItem mEditProfile;
                private MenuItem mCopyProfile;
+               private MenuItem mDeleteProfile;
+
+               private boolean mCanEdit;
+               private boolean mCanCopy;
+               private boolean mCanDelete;
+
+               private int mReadOnlyCount;
 
                @Override
                public boolean onPrepareActionMode(ActionMode mode, Menu menu)
                {
-                       mEditProfile.setEnabled(mSelected.size() == 1);
-                       mCopyProfile.setEnabled(mEditProfile.isEnabled());
+                       mEditProfile.setEnabled(mCanEdit);
+                       mCopyProfile.setEnabled(mCanCopy);
+                       mDeleteProfile.setEnabled(mCanDelete);
                        return true;
                }
 
                @Override
                public void onDestroyActionMode(ActionMode mode)
                {
+                       mReadOnlyCount = 0;
                        mSelected.clear();
                }
 
@@ -259,6 +270,7 @@ public class VpnProfileListFragment extends Fragment
                        inflater.inflate(R.menu.profile_list_context, menu);
                        mEditProfile = menu.findItem(R.id.edit_profile);
                        mCopyProfile = menu.findItem(R.id.copy_profile);
+                       mDeleteProfile = menu.findItem(R.id.delete_profile);
                        mode.setTitle(R.string.select_profiles);
                        return true;
                }
@@ -327,13 +339,17 @@ public class VpnProfileListFragment extends Fragment
                public void onItemCheckedStateChanged(ActionMode mode, int position,
                                                                                          long id, boolean checked)
                {
+                       VpnProfile profile = (VpnProfile)mListView.getItemAtPosition(position);
+
                        if (checked)
                        {
                                mSelected.add(position);
+                               mReadOnlyCount += profile.isReadOnly() ? 1 : 0;
                        }
                        else
                        {
                                mSelected.remove(position);
+                               mReadOnlyCount -= profile.isReadOnly() ? 1 : 0;
                        }
                        final int checkedCount = mSelected.size();
                        switch (checkedCount)
@@ -348,6 +364,11 @@ public class VpnProfileListFragment extends Fragment
                                        mode.setSubtitle(String.format(getString(R.string.x_profiles_selected), checkedCount));
                                        break;
                        }
+
+                       mCanEdit = checkedCount == 1;
+                       mCanCopy = checkedCount == 1 && mReadOnlyCount == 0;
+                       mCanDelete = checkedCount > 0 && mReadOnlyCount == 0;
+
                        mode.invalidate();
                }
        };