android: Disable copy/delete for read-only profiles
If a profile is marked as read-only, do not allow users to copy or delete the profile.
This commit is contained in:
committed by
Tobias Brunner
parent
150dc5ab64
commit
d3f5c3a760
+24
-3
@@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user