Fix fragment - Merge branch 'feature/ListNodesFragment' into feature/NodeMenu

This commit is contained in:
J-Jamet committed 2018-05-25 22:09:35 +02:00
commit 7c54946c4b
19 files changed
+360 -156

No files matched your search

@@ -33,6 +33,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
@@ -50,6 +51,7 @@ import com.kunzisoft.keepass.adapters.NodeAdapter;
import com.kunzisoft.keepass.app.App;
import com.kunzisoft.keepass.autofill.AutofillHelper;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.PwDatabase;
import com.kunzisoft.keepass.database.PwEntry;
import com.kunzisoft.keepass.database.PwGroup;
import com.kunzisoft.keepass.database.PwGroupId;
@@ -58,8 +60,10 @@ import com.kunzisoft.keepass.database.PwIconStandard;
import com.kunzisoft.keepass.database.PwNode;
import com.kunzisoft.keepass.database.action.AddGroupRunnable;
import com.kunzisoft.keepass.database.action.CopyEntryRunnable;
import com.kunzisoft.keepass.database.action.AfterActionNodeOnFinish;
import com.kunzisoft.keepass.database.action.DeleteEntryRunnable;
import com.kunzisoft.keepass.database.action.DeleteGroupRunnable;
import com.kunzisoft.keepass.database.action.OnFinishRunnable;
import com.kunzisoft.keepass.database.action.UpdateGroupRunnable;
import com.kunzisoft.keepass.dialogs.AssignMasterKeyDialogFragment;
import com.kunzisoft.keepass.dialogs.GroupEditDialogFragment;
@@ -69,6 +73,7 @@ import com.kunzisoft.keepass.icons.IconPackChooser;
import com.kunzisoft.keepass.search.SearchResultsActivity;
import com.kunzisoft.keepass.settings.PreferencesUtil;
import com.kunzisoft.keepass.tasks.SaveDatabaseProgressTaskDialogFragment;
import com.kunzisoft.keepass.tasks.UIToastTask;
import com.kunzisoft.keepass.tasks.UpdateProgressTaskStatus;
import com.kunzisoft.keepass.view.AddNodeButtonView;
@@ -80,8 +85,6 @@ public class GroupActivity extends ListNodesActivity
private static final String TAG = GroupActivity.class.getName();
private static final String GROUP_ID_KEY = "GROUP_ID_KEY";
private Toolbar toolbar;
private ImageView iconView;
@@ -164,6 +167,7 @@ public class GroupActivity extends ListNodesActivity
toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
groupNameView = findViewById(R.id.group_name);
addNodeButtonView.setAddGroupClickListener(v -> {
GroupEditDialogFragment.build()
@@ -180,14 +184,30 @@ public class GroupActivity extends ListNodesActivity
}
}
protected PwGroup initializeListNodesFragment() {
PwGroup currentGroup;
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putSerializable(GROUP_ID_KEY, mCurrentGroup.getId());
outState.putSerializable(OLD_GROUP_TO_UPDATE_KEY, oldGroupToUpdate);
super.onSaveInstanceState(outState);
}
protected PwGroup retrieveCurrentGroup(@Nullable Bundle savedInstanceState) {
PwGroupId pwGroupId = null; // TODO Parcelable
if (savedInstanceState != null
&& savedInstanceState.containsKey(GROUP_ID_KEY)) {
pwGroupId = (PwGroupId) savedInstanceState.getSerializable(GROUP_ID_KEY);
} else {
if (getIntent() != null)
pwGroupId = (PwGroupId) getIntent().getSerializableExtra(GROUP_ID_KEY);
}
Database db = App.getDB();
readOnly = db.isReadOnly();
PwGroup root = db.getPwDatabase().getRootGroup();
Log.w(TAG, "Creating tree view");
PwGroupId pwGroupId = (PwGroupId) getIntent().getSerializableExtra(GROUP_ID_KEY);
PwGroup currentGroup;
if ( pwGroupId == null ) {
currentGroup = root;
} else {
@@ -202,8 +222,6 @@ public class GroupActivity extends ListNodesActivity
addEntryEnabled = !isRoot && addEntryEnabled;
}
listNodesFragment = ListNodesFragment.newInstance(currentGroup);
return currentGroup;
}
@@ -223,10 +241,12 @@ public class GroupActivity extends ListNodesActivity
App.getDB().getDrawFactory().assignDatabaseIconTo(this, iconView, mCurrentGroup.getIcon());
}
if ( mCurrentGroup.getParent() != null )
toolbar.setNavigationIcon(R.drawable.ic_arrow_up_white_24dp);
else {
toolbar.setNavigationIcon(null);
if (toolbar != null) {
if ( mCurrentGroup.containsParent() )
toolbar.setNavigationIcon(R.drawable.ic_arrow_up_white_24dp);
else {
toolbar.setNavigationIcon(null);
}
}
}
}
@@ -357,8 +377,6 @@ public class GroupActivity extends ListNodesActivity
@Override
protected void onResume() {
super.onResume();
// Refresh the group icon
assignToolbarElements();
// Show button on resume
if (addNodeButtonView != null)
addNodeButtonView.showButton();
@@ -649,10 +667,91 @@ public class GroupActivity extends ListNodesActivity
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putSerializable(OLD_GROUP_TO_UPDATE_KEY, oldGroupToUpdate);
super.onSaveInstanceState(outState);
class AfterAddNode extends AfterActionNodeOnFinish {
AfterAddNode(Handler handler) {
super(handler);
}
public void run(PwNode oldNode, PwNode newNode) {
super.run();
runOnUiThread(() -> {
if (mSuccess) {
if (listNodesFragment != null)
listNodesFragment.addNode(newNode);
} else {
displayMessage(GroupActivity.this);
}
SaveDatabaseProgressTaskDialogFragment.stop(GroupActivity.this);
});
}
}
class AfterUpdateNode extends AfterActionNodeOnFinish {
AfterUpdateNode(Handler handler) {
super(handler);
}
public void run(PwNode oldNode, PwNode newNode) {
super.run();
runOnUiThread(() -> {
if (mSuccess) {
if (listNodesFragment != null)
listNodesFragment.updateNode(oldNode, newNode);
} else {
displayMessage(GroupActivity.this);
}
SaveDatabaseProgressTaskDialogFragment.stop(GroupActivity.this);
});
}
}
class AfterDeleteNode extends OnFinishRunnable {
private PwNode pwNode;
AfterDeleteNode(Handler handler, PwNode pwNode) {
super(handler);
this.pwNode = pwNode;
}
@Override
public void run() {
super.run();
runOnUiThread(() -> {
if ( mSuccess) {
if (listNodesFragment != null)
listNodesFragment.removeNode(pwNode);
PwGroup parent = pwNode.getParent();
Database db = App.getDB();
PwDatabase database = db.getPwDatabase();
if (db.isRecycleBinAvailable() &&
db.isRecycleBinEnabled()) {
PwGroup recycleBin = database.getRecycleBin();
// Add trash if it doesn't exists
if (parent.equals(recycleBin)
&& mCurrentGroup != null
&& mCurrentGroup.getParent() == null
&& !mCurrentGroup.equals(recycleBin)) {
if (listNodesFragment != null)
listNodesFragment.addNode(parent);
}
}
} else {
mHandler.post(new UIToastTask(GroupActivity.this, "Unrecoverable error: " + mMessage));
App.setShutdown();
finish();
}
SaveDatabaseProgressTaskDialogFragment.stop(GroupActivity.this);
});
}
}
@Override
@@ -25,7 +25,7 @@ import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@@ -35,19 +35,13 @@ import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.adapters.NodeAdapter;
import com.kunzisoft.keepass.app.App;
import com.kunzisoft.keepass.autofill.AutofillHelper;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.PwDatabase;
import com.kunzisoft.keepass.database.PwEntry;
import com.kunzisoft.keepass.database.PwGroup;
import com.kunzisoft.keepass.database.PwNode;
import com.kunzisoft.keepass.database.SortNodeEnum;
import com.kunzisoft.keepass.database.action.AfterActionNodeOnFinish;
import com.kunzisoft.keepass.database.action.OnFinishRunnable;
import com.kunzisoft.keepass.dialogs.AssignMasterKeyDialogFragment;
import com.kunzisoft.keepass.dialogs.SortDialogFragment;
import com.kunzisoft.keepass.password.AssignPasswordHelper;
import com.kunzisoft.keepass.tasks.SaveDatabaseProgressTaskDialogFragment;
import com.kunzisoft.keepass.tasks.UIToastTask;
import com.kunzisoft.keepass.utils.MenuUtil;
public abstract class ListNodesActivity extends LockingActivity
@@ -55,10 +49,13 @@ public abstract class ListNodesActivity extends LockingActivity
NodeAdapter.NodeClickCallback,
SortDialogFragment.SortSelectionListener {
protected static final String GROUP_ID_KEY = "GROUP_ID_KEY";
protected static final String LIST_NODES_FRAGMENT_TAG = "LIST_NODES_FRAGMENT_TAG";
protected ListNodesFragment listNodesFragment;
protected PwGroup mCurrentGroup;
protected TextView groupNameView;
protected AutofillHelper autofillHelper;
@@ -78,7 +75,9 @@ public abstract class ListNodesActivity extends LockingActivity
invalidateOptionsMenu();
mCurrentGroup = initializeListNodesFragment();
mCurrentGroup = retrieveCurrentGroup(savedInstanceState);
initializeListNodesFragment(mCurrentGroup);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
autofillHelper = new AutofillHelper();
@@ -86,7 +85,21 @@ public abstract class ListNodesActivity extends LockingActivity
}
}
protected abstract PwGroup initializeListNodesFragment();
protected abstract PwGroup retrieveCurrentGroup(@Nullable Bundle savedInstanceState);
@Override
protected void onResume() {
super.onResume();
// Refresh the title
assignToolbarElements();
}
protected void initializeListNodesFragment(PwGroup currentGroup) {
listNodesFragment = (ListNodesFragment) getSupportFragmentManager()
.findFragmentByTag(LIST_NODES_FRAGMENT_TAG);
if (listNodesFragment == null)
listNodesFragment = ListNodesFragment.newInstance(currentGroup);
}
/**
* Attach the fragment's list of node.
@@ -104,16 +117,15 @@ public abstract class ListNodesActivity extends LockingActivity
public void assignToolbarElements() {
if (mCurrentGroup != null) {
String title = mCurrentGroup.getName();
TextView tv = findViewById(R.id.group_name);
if (title != null && title.length() > 0) {
if (tv != null) {
tv.setText(title);
tv.invalidate();
if (groupNameView != null) {
groupNameView.setText(title);
groupNameView.invalidate();
}
} else {
if (tv != null) {
tv.setText(getText(R.string.root));
tv.invalidate();
if (groupNameView != null) {
groupNameView.setText(getText(R.string.root));
groupNameView.invalidate();
}
}
}
@@ -239,100 +251,13 @@ public abstract class ListNodesActivity extends LockingActivity
}
}
class AfterAddNode extends AfterActionNodeOnFinish {
AfterAddNode(Handler handler) {
super(handler);
}
public void run(PwNode oldNode, PwNode newNode) {
super.run();
runOnUiThread(() -> {
if (mSuccess) {
if (listNodesFragment != null)
listNodesFragment.addNode(newNode);
} else {
displayMessage(ListNodesActivity.this);
}
SaveDatabaseProgressTaskDialogFragment.stop(ListNodesActivity.this);
});
}
}
class AfterUpdateNode extends AfterActionNodeOnFinish {
AfterUpdateNode(Handler handler) {
super(handler);
}
public void run(PwNode oldNode, PwNode newNode) {
super.run();
runOnUiThread(() -> {
if (mSuccess) {
if (listNodesFragment != null)
listNodesFragment.updateNode(oldNode, newNode);
} else {
displayMessage(ListNodesActivity.this);
}
SaveDatabaseProgressTaskDialogFragment.stop(ListNodesActivity.this);
});
}
}
class AfterDeleteNode extends OnFinishRunnable {
private PwNode pwNode;
AfterDeleteNode(Handler handler, PwNode pwNode) {
super(handler);
this.pwNode = pwNode;
}
@Override
public void run() {
super.run();
runOnUiThread(() -> {
if ( mSuccess) {
if (listNodesFragment != null)
listNodesFragment.removeNode(pwNode);
PwGroup parent = pwNode.getParent();
Database db = App.getDB();
PwDatabase database = db.getPwDatabase();
if (db.isRecycleBinAvailable() &&
db.isRecycleBinEnabled()) {
PwGroup recycleBin = database.getRecycleBin();
// Add trash if it doesn't exists
if (parent.equals(recycleBin)
&& mCurrentGroup != null
&& mCurrentGroup.getParent() == null
&& !mCurrentGroup.equals(recycleBin)) {
if (listNodesFragment != null)
listNodesFragment.addNode(parent);
}
}
} else {
mHandler.post(new UIToastTask(ListNodesActivity.this, "Unrecoverable error: " + mMessage));
App.setShutdown();
finish();
}
SaveDatabaseProgressTaskDialogFragment.stop(ListNodesActivity.this);
});
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
listNodesFragment = (ListNodesFragment) getSupportFragmentManager().findFragmentByTag(LIST_NODES_FRAGMENT_TAG);
// to refresh fragment
listNodesFragment.onResume();
listNodesFragment.rebuildList();
mCurrentGroup = listNodesFragment.getMainGroup();
assignToolbarElements();
}
@@ -7,7 +7,6 @@ import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
@@ -29,8 +28,9 @@ import com.kunzisoft.keepass.database.PwNode;
import com.kunzisoft.keepass.database.SortNodeEnum;
import com.kunzisoft.keepass.dialogs.SortDialogFragment;
import com.kunzisoft.keepass.settings.PreferencesUtil;
import com.kunzisoft.keepass.stylish.StylishFragment;
public class ListNodesFragment extends Fragment implements
public class ListNodesFragment extends StylishFragment implements
SortDialogFragment.SortSelectionListener {
private static final String TAG = ListNodesFragment.class.getName();
@@ -104,7 +104,7 @@ public class ListNodesFragment extends Fragment implements
setHasOptionsMenu(true);
mCurrentGroup = initCurrentGroup();
mAdapter = new NodeAdapter(getContext());
mAdapter = new NodeAdapter(getContextThemed());
mAdapter.setOnNodeClickListener(nodeClickCallback);
if (nodeMenuListener != null) {
@@ -115,16 +115,18 @@ public class ListNodesFragment extends Fragment implements
prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
}
protected PwGroup initCurrentGroup() {
protected PwGroup initCurrentGroup() { // TODO Change by parcelable
Database db = App.getDB();
PwGroup root = db.getPwDatabase().getRootGroup();
PwGroup currentGroup = null;
PwGroup currentGroup = null;
if (getArguments() != null) {
// Contains all the group in element
if (getArguments().containsKey(GROUP_KEY)) {
currentGroup = (PwGroup) getArguments().getSerializable(GROUP_KEY);
}
// Contains only the group id, so the group must be retrieve
if (getArguments().containsKey(GROUP_ID_KEY)) {
PwGroupId pwGroupId = (PwGroupId) getArguments().getSerializable(GROUP_ID_KEY);
if ( pwGroupId != null )
@@ -142,8 +144,11 @@ public class ListNodesFragment extends Fragment implements
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.list_nodes_fragment, container, false);
// To apply theme
View rootView = inflater.cloneInContext(getContextThemed())
.inflate(R.layout.list_nodes_fragment, container, false);
listView = rootView.findViewById(R.id.nodes_list);
if (onScrollListener != null) {
@@ -163,6 +168,10 @@ public class ListNodesFragment extends Fragment implements
public void onResume() {
super.onResume();
rebuildList();
}
public void rebuildList() {
// Add elements to the list
mAdapter.rebuildList(mCurrentGroup);
assignListToNodeAdapter(listView);
@@ -20,8 +20,6 @@
package com.kunzisoft.keepass.adapters;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.kunzisoft.keepass.R;
@@ -30,7 +28,7 @@ class EntryViewHolder extends BasicViewHolder {
EntryViewHolder(View itemView) {
super(itemView);
container = itemView.findViewById(R.id.entry_container);
icon = (ImageView) itemView.findViewById(R.id.entry_icon);
text = (TextView) itemView.findViewById(R.id.entry_text);
icon = itemView.findViewById(R.id.entry_icon);
text = itemView.findViewById(R.id.entry_text);
}
}
@@ -20,8 +20,6 @@
package com.kunzisoft.keepass.adapters;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.kunzisoft.keepass.R;
@@ -30,7 +28,7 @@ class GroupViewHolder extends BasicViewHolder {
GroupViewHolder(View itemView) {
super(itemView);
container = itemView.findViewById(R.id.group_container);
icon = (ImageView) itemView.findViewById(R.id.group_icon);
text = (TextView) itemView.findViewById(R.id.group_text);
icon = itemView.findViewById(R.id.group_icon);
text = itemView.findViewById(R.id.group_text);
}
}
@@ -119,6 +119,13 @@ public abstract class PwNode<Parent extends PwGroup> implements ISmallTimeLogger
parent = prt;
}
/**
* @return true if parent is present (can be a root or a detach element)
*/
public boolean containsParent() {
return getParent() != null;
}
public PwDate getCreationTime() {
return creation;
}
@@ -30,12 +30,13 @@ import android.view.View;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.activities.ListNodesActivity;
import com.kunzisoft.keepass.activities.ListNodesFragment;
import com.kunzisoft.keepass.app.App;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.PwGroup;
import com.kunzisoft.keepass.utils.MenuUtil;
import javax.annotation.Nullable;
public class SearchResultsActivity extends ListNodesActivity {
@Override
@@ -50,6 +51,7 @@ public class SearchResultsActivity extends ListNodesActivity {
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
groupNameView = findViewById(R.id.group_name);
attachFragmentToContentView();
@@ -66,15 +68,13 @@ public class SearchResultsActivity extends ListNodesActivity {
}
@Override
protected PwGroup initializeListNodesFragment() {
protected PwGroup retrieveCurrentGroup(@Nullable Bundle savedInstanceState) {
Database mDb = App.getDB();
// Likely the app has been killed exit the activity
if ( ! mDb.getLoaded() ) {
finish();
}
PwGroup group = mDb.search(getSearchStr(getIntent()).trim());
listNodesFragment = ListNodesFragment.newInstance(group);
return group;
return mDb.search(getSearchStr(getIntent()).trim());
}
@Override
@@ -112,8 +112,8 @@ public class SettingsActivity extends LockingActivity implements MainPreferenceF
@Override
public void onNestedPreferenceSelected(NestedSettingsFragment.Screen key) {
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left,
R.anim.slide_in_left, R.anim.slide_out_right)
.setCustomAnimations(R.anim.slide_alpha_in_right, R.anim.slide_alpha_out_left,
R.anim.slide_alpha_in_left, R.anim.slide_alpha_out_right)
.replace(R.id.fragment_container, NestedSettingsFragment.newInstance(key), TAG_NESTED)
.addToBackStack(TAG_NESTED)
.commit();
@@ -0,0 +1,71 @@
/*
* Copyright 2018 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.stylish;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v4.app.Fragment;
import android.support.v7.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
public abstract class StylishFragment extends Fragment {
protected @StyleRes int themeId;
protected Context contextThemed; // TODO small ref
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context != null) {
this.themeId = Stylish.getThemeId(context);
}
contextThemed = new ContextThemeWrapper(context, themeId);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// To fix status bar color
if (getActivity() != null
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getActivity().getWindow();
int[] attrColorPrimaryDark = {android.R.attr.colorPrimaryDark};
TypedArray taColorPrimaryDark = contextThemed.getTheme().obtainStyledAttributes(attrColorPrimaryDark);
window.setStatusBarColor(taColorPrimaryDark.getColor(0, Color.BLACK));
taColorPrimaryDark.recycle();
}
return super.onCreateView(inflater, container, savedInstanceState);
}
public Context getContextThemed() {
return contextThemed;
}
}