#77 Add read-only mode to the floating menu

This commit is contained in:
J-Jamet
2018-07-18 16:29:18 +02:00
parent ccca9c4400
commit 92fb22129c
9 changed files with 52 additions and 45 deletions

View File

@@ -78,7 +78,6 @@ public class EntryActivity extends LockingHideActivity {
protected PwEntry mEntry;
private boolean mShowPassword;
protected boolean readOnly = false;
private ClipboardHelper clipboardHelper;
private boolean firstLaunchOfActivity;
@@ -105,8 +104,6 @@ public class EntryActivity extends LockingHideActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrIntent(savedInstanceState, getIntent());
Database db = App.getDB();
// Likely the app has been killed exit the activity
if ( ! db.getLoaded() ) {
@@ -233,12 +230,6 @@ public class EntryActivity extends LockingHideActivity {
firstLaunchOfActivity = false;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
ReadOnlyHelper.onSaveInstanceState(outState, readOnly);
super.onSaveInstanceState(outState);
}
/**
* Check and display learning views
* Displays the explanation for copying a field and editing an entry

View File

@@ -57,8 +57,6 @@ public abstract class ListNodesActivity extends LockingActivity
protected PwGroup mCurrentGroup;
protected TextView groupNameView;
protected boolean readOnly;
protected boolean entrySelectionMode;
protected AutofillHelper autofillHelper;
@@ -76,8 +74,6 @@ public abstract class ListNodesActivity extends LockingActivity
return;
}
readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrIntent(savedInstanceState, getIntent());
invalidateOptionsMenu();
mCurrentGroup = retrieveCurrentGroup(savedInstanceState);
@@ -91,12 +87,6 @@ public abstract class ListNodesActivity extends LockingActivity
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
ReadOnlyHelper.onSaveInstanceState(outState, readOnly);
super.onSaveInstanceState(outState);
}
protected abstract PwGroup retrieveCurrentGroup(@Nullable Bundle savedInstanceState);
@Override
@@ -110,7 +100,7 @@ public abstract class ListNodesActivity extends LockingActivity
listNodesFragment = (ListNodesFragment) getSupportFragmentManager()
.findFragmentByTag(LIST_NODES_FRAGMENT_TAG);
if (listNodesFragment == null)
listNodesFragment = ListNodesFragment.newInstance(currentGroup.getId());
listNodesFragment = ListNodesFragment.newInstance(currentGroup.getId(), readOnly);
}
/**
@@ -213,7 +203,7 @@ public abstract class ListNodesActivity extends LockingActivity
if (checkTimeIsAllowedOrFinish(this)) {
startRecordTime(this);
ListNodesFragment newListNodeFragment = ListNodesFragment.newInstance(group.getId());
ListNodesFragment newListNodeFragment = ListNodesFragment.newInstance(group.getId(), readOnly);
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left,
R.anim.slide_in_left, R.anim.slide_out_right)

View File

@@ -49,21 +49,25 @@ public class ListNodesFragment extends StylishFragment implements
// Preferences for sorting
private SharedPreferences prefs;
public static ListNodesFragment newInstance(PwGroup group) {
private boolean readOnly;
public static ListNodesFragment newInstance(PwGroup group, boolean readOnly) {
Bundle bundle = new Bundle();
if (group != null) {
bundle.putParcelable(GROUP_KEY, group);
}
ReadOnlyHelper.putReadOnlyInBundle(bundle, readOnly);
ListNodesFragment listNodesFragment = new ListNodesFragment();
listNodesFragment.setArguments(bundle);
return listNodesFragment;
}
public static ListNodesFragment newInstance(PwGroupId groupId) {
public static ListNodesFragment newInstance(PwGroupId groupId, boolean readOnly) {
Bundle bundle=new Bundle();
if (groupId != null) {
bundle.putParcelable(GROUP_ID_KEY, groupId);
}
ReadOnlyHelper.putReadOnlyInBundle(bundle, readOnly);
ListNodesFragment listNodesFragment = new ListNodesFragment();
listNodesFragment.setArguments(bundle);
return listNodesFragment;
@@ -103,9 +107,11 @@ public class ListNodesFragment extends StylishFragment implements
setHasOptionsMenu(true);
readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrArguments(savedInstanceState, getArguments());
mCurrentGroup = initCurrentGroup();
if (getActivity() != null) {
mAdapter = new NodeAdapter(getContextThemed(), getActivity().getMenuInflater());
mAdapter = new NodeAdapter(getContextThemed(), getActivity().getMenuInflater(), readOnly);
mAdapter.setOnNodeClickListener(nodeClickCallback);
if (nodeMenuListener != null) {
@@ -117,6 +123,12 @@ public class ListNodesFragment extends StylishFragment implements
prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
ReadOnlyHelper.onSaveInstanceState(outState, readOnly);
super.onSaveInstanceState(outState);
}
protected PwGroup initCurrentGroup() {
Database db = App.getDB();

View File

@@ -61,6 +61,7 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
private NodeClickCallback nodeClickCallback;
private NodeMenuListener nodeMenuListener;
private boolean activateContextMenu;
private boolean readOnly;
private int iconGroupColor;
private int iconEntryColor;
@@ -69,12 +70,13 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
* Create node list adapter with contextMenu or not
* @param context Context to use
*/
public NodeAdapter(final Context context, MenuInflater menuInflater) {
public NodeAdapter(final Context context, MenuInflater menuInflater, boolean readOnly) {
this.inflater = LayoutInflater.from(context);
this.menuInflater = menuInflater;
this.context = context;
assignPreferences();
this.activateContextMenu = false;
this.readOnly = readOnly;
this.nodeSortedList = new SortedList<>(PwNode.class, new SortedListAdapterCallback<PwNode>(this) {
@Override public int compare(PwNode item1, PwNode item2) {
@@ -217,7 +219,7 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
// Context menu
if (activateContextMenu) {
holder.container.setOnCreateContextMenuListener(
new ContextMenuBuilder(subNode, nodeMenuListener));
new ContextMenuBuilder(subNode, nodeMenuListener, readOnly));
}
// Assign image and text size
// Relative size of the icon
@@ -287,24 +289,26 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
private PwNode node;
private NodeMenuListener menuListener;
private boolean readOnly;
ContextMenuBuilder(PwNode node, NodeMenuListener menuListener) {
ContextMenuBuilder(PwNode node, NodeMenuListener menuListener, boolean readOnly) {
this.menuListener = menuListener;
this.node = node;
this.readOnly = readOnly;
}
@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
menuInflater.inflate(R.menu.node_menu, contextMenu);
// TODO COPY For Group
if (node.getType().equals(PwNode.Type.GROUP)) {
contextMenu.removeItem(R.id.menu_copy);
}
MenuItem menuItem = contextMenu.findItem(R.id.menu_open);
menuItem.setOnMenuItemClickListener(mOnMyActionClickListener);
if (!App.getDB().isReadOnly() && !node.equals(App.getDB().getPwDatabase().getRecycleBin())) {
if (readOnly || node.equals(App.getDB().getPwDatabase().getRecycleBin())) {
contextMenu.removeItem(R.id.menu_edit);
contextMenu.removeItem(R.id.menu_copy);
contextMenu.removeItem(R.id.menu_move);
contextMenu.removeItem(R.id.menu_delete);
} else {
// Edition
menuItem = contextMenu.findItem(R.id.menu_edit);
menuItem.setOnMenuItemClickListener(mOnMyActionClickListener);
@@ -312,6 +316,9 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
if (node.getType().equals(PwNode.Type.ENTRY)) {
menuItem = contextMenu.findItem(R.id.menu_copy);
menuItem.setOnMenuItemClickListener(mOnMyActionClickListener);
} else {
// TODO COPY For Group
contextMenu.removeItem(R.id.menu_copy);
}
// Move
menuItem = contextMenu.findItem(R.id.menu_move);

View File

@@ -29,6 +29,7 @@ import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import com.kunzisoft.keepass.activities.ReadOnlyHelper;
import com.kunzisoft.keepass.app.App;
import com.kunzisoft.keepass.settings.PreferencesUtil;
import com.kunzisoft.keepass.stylish.StylishActivity;
@@ -45,6 +46,7 @@ public abstract class LockingActivity extends StylishActivity {
private LockReceiver lockReceiver;
private boolean exitLock;
protected boolean readOnly;
/**
* Called to start a record time,
@@ -72,6 +74,9 @@ public abstract class LockingActivity extends StylishActivity {
lockReceiver = null;
exitLock = false;
readOnly = false;
readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrIntent(savedInstanceState, getIntent());
}
public static void checkShutdown(Activity activity) {
@@ -116,6 +121,12 @@ public abstract class LockingActivity extends StylishActivity {
TimeoutHelper.recordTime(this);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
ReadOnlyHelper.onSaveInstanceState(outState, readOnly);
super.onSaveInstanceState(outState);
}
@Override
protected void onPause() {
super.onPause();

View File

@@ -74,7 +74,7 @@ public class SearchResultsActivity extends ListNodesActivity {
.findFragmentByTag(LIST_NODES_FRAGMENT_TAG);
// Directly get group and not id
if (listNodesFragment == null)
listNodesFragment = ListNodesFragment.newInstance(currentGroup);
listNodesFragment = ListNodesFragment.newInstance(currentGroup, readOnly);
}
@Override

View File

@@ -552,6 +552,12 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
ReadOnlyHelper.onSaveInstanceState(outState, databaseReadOnly);
super.onSaveInstanceState(outState);
}
@Override
public boolean onPreferenceClick(Preference preference) {
// TODO encapsulate

View File

@@ -40,8 +40,6 @@ public class SettingsActivity extends LockingActivity implements MainPreferenceF
private Toolbar toolbar;
private boolean readOnly;
public static void launch(Activity activity, boolean readOnly) {
Intent intent = new Intent(activity, SettingsActivity.class);
ReadOnlyHelper.putReadOnlyInIntent(intent, readOnly);
@@ -76,8 +74,6 @@ public class SettingsActivity extends LockingActivity implements MainPreferenceF
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrIntent(savedInstanceState, getIntent());
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, retrieveMainFragment())
@@ -87,12 +83,6 @@ public class SettingsActivity extends LockingActivity implements MainPreferenceF
backupManager = new BackupManager(this);
}
@Override
public void onSaveInstanceState(Bundle outState) {
ReadOnlyHelper.onSaveInstanceState(outState, readOnly);
super.onSaveInstanceState(outState);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch ( item.getItemId() ) {

View File

@@ -283,7 +283,7 @@
<string name="allow_no_password_title">Allow no password</string>
<string name="allow_no_password_summary">Enable the open button if no password identification is selected.</string>
<string name="enable_read_only_title">Read only</string>
<string name="enable_read_only_summary">By default, open a read-only database.</string>
<string name="enable_read_only_summary">By default, open a database in read-only mode.</string>
<string name="enable_education_screens_title">Education screens</string>
<string name="enable_education_screens_summary">Highlight the elements to learn how the application works</string>