#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; protected PwEntry mEntry;
private boolean mShowPassword; private boolean mShowPassword;
protected boolean readOnly = false;
private ClipboardHelper clipboardHelper; private ClipboardHelper clipboardHelper;
private boolean firstLaunchOfActivity; private boolean firstLaunchOfActivity;
@@ -105,8 +104,6 @@ public class EntryActivity extends LockingHideActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true);
readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrIntent(savedInstanceState, getIntent());
Database db = App.getDB(); Database db = App.getDB();
// Likely the app has been killed exit the activity // Likely the app has been killed exit the activity
if ( ! db.getLoaded() ) { if ( ! db.getLoaded() ) {
@@ -233,12 +230,6 @@ public class EntryActivity extends LockingHideActivity {
firstLaunchOfActivity = false; firstLaunchOfActivity = false;
} }
@Override
protected void onSaveInstanceState(Bundle outState) {
ReadOnlyHelper.onSaveInstanceState(outState, readOnly);
super.onSaveInstanceState(outState);
}
/** /**
* Check and display learning views * Check and display learning views
* Displays the explanation for copying a field and editing an entry * 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 PwGroup mCurrentGroup;
protected TextView groupNameView; protected TextView groupNameView;
protected boolean readOnly;
protected boolean entrySelectionMode; protected boolean entrySelectionMode;
protected AutofillHelper autofillHelper; protected AutofillHelper autofillHelper;
@@ -76,8 +74,6 @@ public abstract class ListNodesActivity extends LockingActivity
return; return;
} }
readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrIntent(savedInstanceState, getIntent());
invalidateOptionsMenu(); invalidateOptionsMenu();
mCurrentGroup = retrieveCurrentGroup(savedInstanceState); 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); protected abstract PwGroup retrieveCurrentGroup(@Nullable Bundle savedInstanceState);
@Override @Override
@@ -110,7 +100,7 @@ public abstract class ListNodesActivity extends LockingActivity
listNodesFragment = (ListNodesFragment) getSupportFragmentManager() listNodesFragment = (ListNodesFragment) getSupportFragmentManager()
.findFragmentByTag(LIST_NODES_FRAGMENT_TAG); .findFragmentByTag(LIST_NODES_FRAGMENT_TAG);
if (listNodesFragment == null) 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)) { if (checkTimeIsAllowedOrFinish(this)) {
startRecordTime(this); startRecordTime(this);
ListNodesFragment newListNodeFragment = ListNodesFragment.newInstance(group.getId()); ListNodesFragment newListNodeFragment = ListNodesFragment.newInstance(group.getId(), readOnly);
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left,
R.anim.slide_in_left, R.anim.slide_out_right) 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 // Preferences for sorting
private SharedPreferences prefs; private SharedPreferences prefs;
public static ListNodesFragment newInstance(PwGroup group) { private boolean readOnly;
public static ListNodesFragment newInstance(PwGroup group, boolean readOnly) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
if (group != null) { if (group != null) {
bundle.putParcelable(GROUP_KEY, group); bundle.putParcelable(GROUP_KEY, group);
} }
ReadOnlyHelper.putReadOnlyInBundle(bundle, readOnly);
ListNodesFragment listNodesFragment = new ListNodesFragment(); ListNodesFragment listNodesFragment = new ListNodesFragment();
listNodesFragment.setArguments(bundle); listNodesFragment.setArguments(bundle);
return listNodesFragment; return listNodesFragment;
} }
public static ListNodesFragment newInstance(PwGroupId groupId) { public static ListNodesFragment newInstance(PwGroupId groupId, boolean readOnly) {
Bundle bundle=new Bundle(); Bundle bundle=new Bundle();
if (groupId != null) { if (groupId != null) {
bundle.putParcelable(GROUP_ID_KEY, groupId); bundle.putParcelable(GROUP_ID_KEY, groupId);
} }
ReadOnlyHelper.putReadOnlyInBundle(bundle, readOnly);
ListNodesFragment listNodesFragment = new ListNodesFragment(); ListNodesFragment listNodesFragment = new ListNodesFragment();
listNodesFragment.setArguments(bundle); listNodesFragment.setArguments(bundle);
return listNodesFragment; return listNodesFragment;
@@ -103,9 +107,11 @@ public class ListNodesFragment extends StylishFragment implements
setHasOptionsMenu(true); setHasOptionsMenu(true);
readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrArguments(savedInstanceState, getArguments());
mCurrentGroup = initCurrentGroup(); mCurrentGroup = initCurrentGroup();
if (getActivity() != null) { if (getActivity() != null) {
mAdapter = new NodeAdapter(getContextThemed(), getActivity().getMenuInflater()); mAdapter = new NodeAdapter(getContextThemed(), getActivity().getMenuInflater(), readOnly);
mAdapter.setOnNodeClickListener(nodeClickCallback); mAdapter.setOnNodeClickListener(nodeClickCallback);
if (nodeMenuListener != null) { if (nodeMenuListener != null) {
@@ -117,6 +123,12 @@ public class ListNodesFragment extends StylishFragment implements
prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
} }
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
ReadOnlyHelper.onSaveInstanceState(outState, readOnly);
super.onSaveInstanceState(outState);
}
protected PwGroup initCurrentGroup() { protected PwGroup initCurrentGroup() {
Database db = App.getDB(); Database db = App.getDB();

View File

@@ -61,6 +61,7 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
private NodeClickCallback nodeClickCallback; private NodeClickCallback nodeClickCallback;
private NodeMenuListener nodeMenuListener; private NodeMenuListener nodeMenuListener;
private boolean activateContextMenu; private boolean activateContextMenu;
private boolean readOnly;
private int iconGroupColor; private int iconGroupColor;
private int iconEntryColor; private int iconEntryColor;
@@ -69,12 +70,13 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
* Create node list adapter with contextMenu or not * Create node list adapter with contextMenu or not
* @param context Context to use * @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.inflater = LayoutInflater.from(context);
this.menuInflater = menuInflater; this.menuInflater = menuInflater;
this.context = context; this.context = context;
assignPreferences(); assignPreferences();
this.activateContextMenu = false; this.activateContextMenu = false;
this.readOnly = readOnly;
this.nodeSortedList = new SortedList<>(PwNode.class, new SortedListAdapterCallback<PwNode>(this) { this.nodeSortedList = new SortedList<>(PwNode.class, new SortedListAdapterCallback<PwNode>(this) {
@Override public int compare(PwNode item1, PwNode item2) { @Override public int compare(PwNode item1, PwNode item2) {
@@ -217,7 +219,7 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
// Context menu // Context menu
if (activateContextMenu) { if (activateContextMenu) {
holder.container.setOnCreateContextMenuListener( holder.container.setOnCreateContextMenuListener(
new ContextMenuBuilder(subNode, nodeMenuListener)); new ContextMenuBuilder(subNode, nodeMenuListener, readOnly));
} }
// Assign image and text size // Assign image and text size
// Relative size of the icon // Relative size of the icon
@@ -287,24 +289,26 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
private PwNode node; private PwNode node;
private NodeMenuListener menuListener; private NodeMenuListener menuListener;
private boolean readOnly;
ContextMenuBuilder(PwNode node, NodeMenuListener menuListener) { ContextMenuBuilder(PwNode node, NodeMenuListener menuListener, boolean readOnly) {
this.menuListener = menuListener; this.menuListener = menuListener;
this.node = node; this.node = node;
this.readOnly = readOnly;
} }
@Override @Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) { public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
menuInflater.inflate(R.menu.node_menu, contextMenu); 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 menuItem = contextMenu.findItem(R.id.menu_open);
menuItem.setOnMenuItemClickListener(mOnMyActionClickListener); 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 // Edition
menuItem = contextMenu.findItem(R.id.menu_edit); menuItem = contextMenu.findItem(R.id.menu_edit);
menuItem.setOnMenuItemClickListener(mOnMyActionClickListener); menuItem.setOnMenuItemClickListener(mOnMyActionClickListener);
@@ -312,6 +316,9 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
if (node.getType().equals(PwNode.Type.ENTRY)) { if (node.getType().equals(PwNode.Type.ENTRY)) {
menuItem = contextMenu.findItem(R.id.menu_copy); menuItem = contextMenu.findItem(R.id.menu_copy);
menuItem.setOnMenuItemClickListener(mOnMyActionClickListener); menuItem.setOnMenuItemClickListener(mOnMyActionClickListener);
} else {
// TODO COPY For Group
contextMenu.removeItem(R.id.menu_copy);
} }
// Move // Move
menuItem = contextMenu.findItem(R.id.menu_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.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import com.kunzisoft.keepass.activities.ReadOnlyHelper;
import com.kunzisoft.keepass.app.App; import com.kunzisoft.keepass.app.App;
import com.kunzisoft.keepass.settings.PreferencesUtil; import com.kunzisoft.keepass.settings.PreferencesUtil;
import com.kunzisoft.keepass.stylish.StylishActivity; import com.kunzisoft.keepass.stylish.StylishActivity;
@@ -45,6 +46,7 @@ public abstract class LockingActivity extends StylishActivity {
private LockReceiver lockReceiver; private LockReceiver lockReceiver;
private boolean exitLock; private boolean exitLock;
protected boolean readOnly;
/** /**
* Called to start a record time, * Called to start a record time,
@@ -72,6 +74,9 @@ public abstract class LockingActivity extends StylishActivity {
lockReceiver = null; lockReceiver = null;
exitLock = false; exitLock = false;
readOnly = false;
readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrIntent(savedInstanceState, getIntent());
} }
public static void checkShutdown(Activity activity) { public static void checkShutdown(Activity activity) {
@@ -116,6 +121,12 @@ public abstract class LockingActivity extends StylishActivity {
TimeoutHelper.recordTime(this); TimeoutHelper.recordTime(this);
} }
@Override
protected void onSaveInstanceState(Bundle outState) {
ReadOnlyHelper.onSaveInstanceState(outState, readOnly);
super.onSaveInstanceState(outState);
}
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();

View File

@@ -74,7 +74,7 @@ public class SearchResultsActivity extends ListNodesActivity {
.findFragmentByTag(LIST_NODES_FRAGMENT_TAG); .findFragmentByTag(LIST_NODES_FRAGMENT_TAG);
// Directly get group and not id // Directly get group and not id
if (listNodesFragment == null) if (listNodesFragment == null)
listNodesFragment = ListNodesFragment.newInstance(currentGroup); listNodesFragment = ListNodesFragment.newInstance(currentGroup, readOnly);
} }
@Override @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 @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
// TODO encapsulate // TODO encapsulate

View File

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

View File

@@ -283,7 +283,7 @@
<string name="allow_no_password_title">Allow no password</string> <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="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_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_title">Education screens</string>
<string name="enable_education_screens_summary">Highlight the elements to learn how the application works</string> <string name="enable_education_screens_summary">Highlight the elements to learn how the application works</string>