From d6bac74e1b6409925e2f8d050ed8755df0348fac Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Sun, 7 Jul 2019 20:01:17 +0200 Subject: [PATCH] Kotlinized GroupActivity --- .../keepass/activities/GroupActivity.java | 1135 ----------------- .../keepass/activities/GroupActivity.kt | 1051 +++++++++++++++ .../AssignPasswordInDatabaseRunnable.kt | 5 +- .../FileDatabaseSelectActivity.java | 6 +- .../IntentBuildLauncher.java | 2 +- .../keepass/password/PasswordActivity.java | 7 +- 6 files changed, 1060 insertions(+), 1146 deletions(-) delete mode 100644 app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.java create mode 100644 app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt rename app/src/main/java/com/kunzisoft/keepass/{activities => password}/IntentBuildLauncher.java (73%) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.java b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.java deleted file mode 100644 index 931019011..000000000 --- a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.java +++ /dev/null @@ -1,1135 +0,0 @@ -/* - * Copyright 2017 Brian Pellin, 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 . - * - */ -package com.kunzisoft.keepass.activities; - -import android.annotation.SuppressLint; -import android.app.Activity; -import android.app.Dialog; -import android.app.SearchManager; -import android.app.assist.AssistStructure; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.content.res.TypedArray; -import android.graphics.Color; -import android.net.Uri; -import android.os.Build; -import android.os.Bundle; -import android.os.Handler; -import android.preference.PreferenceManager; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.annotation.RequiresApi; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentTransaction; -import android.support.v7.widget.SearchView; -import android.support.v7.widget.Toolbar; -import android.util.Log; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; -import android.widget.ImageView; -import android.widget.TextView; - -import com.kunzisoft.keepass.R; -import com.kunzisoft.keepass.activities.lock.LockingActivity; -import com.kunzisoft.keepass.adapters.NodeAdapter; -import com.kunzisoft.keepass.adapters.SearchEntryCursorAdapter; -import com.kunzisoft.keepass.app.App; -import com.kunzisoft.keepass.autofill.AutofillHelper; -import com.kunzisoft.keepass.database.SortNodeEnum; -import com.kunzisoft.keepass.database.action.AssignPasswordInDatabaseRunnable; -import com.kunzisoft.keepass.database.action.ProgressDialogRunnable; -import com.kunzisoft.keepass.database.action.node.ActionNodeValues; -import com.kunzisoft.keepass.database.action.node.AddGroupRunnable; -import com.kunzisoft.keepass.database.action.node.AfterActionNodeFinishRunnable; -import com.kunzisoft.keepass.database.action.node.CopyEntryRunnable; -import com.kunzisoft.keepass.database.action.node.DeleteEntryRunnable; -import com.kunzisoft.keepass.database.action.node.DeleteGroupRunnable; -import com.kunzisoft.keepass.database.action.node.MoveEntryRunnable; -import com.kunzisoft.keepass.database.action.node.MoveGroupRunnable; -import com.kunzisoft.keepass.database.action.node.UpdateGroupRunnable; -import com.kunzisoft.keepass.database.element.Database; -import com.kunzisoft.keepass.database.element.EntryVersioned; -import com.kunzisoft.keepass.database.element.GroupVersioned; -import com.kunzisoft.keepass.database.element.NodeVersioned; -import com.kunzisoft.keepass.database.element.PwIcon; -import com.kunzisoft.keepass.database.element.PwNodeId; -import com.kunzisoft.keepass.database.element.security.ProtectedString; -import com.kunzisoft.keepass.dialogs.AssignMasterKeyDialogFragment; -import com.kunzisoft.keepass.dialogs.GroupEditDialogFragment; -import com.kunzisoft.keepass.dialogs.IconPickerDialogFragment; -import com.kunzisoft.keepass.dialogs.PasswordEncodingDialogHelper; -import com.kunzisoft.keepass.dialogs.ReadOnlyDialog; -import com.kunzisoft.keepass.dialogs.SortDialogFragment; -import com.kunzisoft.keepass.education.GroupActivityEducation; -import com.kunzisoft.keepass.magikeyboard.KeyboardEntryNotificationService; -import com.kunzisoft.keepass.magikeyboard.KeyboardHelper; -import com.kunzisoft.keepass.magikeyboard.MagikIME; -import com.kunzisoft.keepass.model.Entry; -import com.kunzisoft.keepass.model.Field; -import com.kunzisoft.keepass.settings.PreferencesUtil; -import com.kunzisoft.keepass.timeout.TimeoutHelper; -import com.kunzisoft.keepass.utils.MenuUtil; -import com.kunzisoft.keepass.view.AddNodeButtonView; - -import net.cachapa.expandablelayout.ExpandableLayout; - -import org.jetbrains.annotations.NotNull; - -import kotlin.Unit; -import kotlin.jvm.functions.Function2; - -public class GroupActivity extends LockingActivity - implements GroupEditDialogFragment.EditGroupListener, - IconPickerDialogFragment.IconPickerListener, - NodeAdapter.NodeMenuListener, - ListNodesFragment.OnScrollListener, - AssignMasterKeyDialogFragment.AssignPasswordDialogListener, - NodeAdapter.NodeClickCallback, - SortDialogFragment.SortSelectionListener { - - private static final String TAG = GroupActivity.class.getName(); - - private static final String GROUP_ID_KEY = "GROUP_ID_KEY"; - private static final String LIST_NODES_FRAGMENT_TAG = "LIST_NODES_FRAGMENT_TAG"; - private static final String SEARCH_FRAGMENT_TAG = "SEARCH_FRAGMENT_TAG"; - private static final String OLD_GROUP_TO_UPDATE_KEY = "OLD_GROUP_TO_UPDATE_KEY"; - private static final String NODE_TO_COPY_KEY = "NODE_TO_COPY_KEY"; - private static final String NODE_TO_MOVE_KEY = "NODE_TO_MOVE_KEY"; - - private Toolbar toolbar; - private View searchTitleView; - private ExpandableLayout toolbarPasteExpandableLayout; - private Toolbar toolbarPaste; - private ImageView iconView; - private TextView modeTitleView; - private AddNodeButtonView addNodeButtonView; - private TextView groupNameView; - - private Database database; - - private ListNodesFragment listNodesFragment; - private boolean currentGroupIsASearch; - - private GroupVersioned rootGroup; - private GroupVersioned mCurrentGroup; - private GroupVersioned oldGroupToUpdate; - private NodeVersioned nodeToCopy; - private NodeVersioned nodeToMove; - - private SearchEntryCursorAdapter searchSuggestionAdapter; - - private int iconColor; - - private static void buildAndLaunchIntent(Activity activity, GroupVersioned group, boolean readOnly, - IntentBuildLauncher intentBuildLauncher) { - if (TimeoutHelper.INSTANCE.checkTimeAndLockIfTimeout(activity)) { - Intent intent = new Intent(activity, GroupActivity.class); - if (group != null) { - intent.putExtra(GROUP_ID_KEY, group.getNodeId()); - } - ReadOnlyHelper.INSTANCE.putReadOnlyInIntent(intent, readOnly); - intentBuildLauncher.launchActivity(intent); - } - } - - /* - * ------------------------- - * Standard Launch - * ------------------------- - */ - - public static void launch(Activity activity) { - launch(activity, PreferencesUtil.enableReadOnlyDatabase(activity)); - } - - public static void launch(Activity activity, boolean readOnly) { - launch(activity, null, readOnly); - } - - public static void launch(Activity activity, GroupVersioned group, boolean readOnly) { - TimeoutHelper.INSTANCE.recordTime(activity); - buildAndLaunchIntent(activity, group, readOnly, - (intent) -> activity.startActivityForResult(intent, 0)); - } - - - /* - * ------------------------- - * Keyboard Launch - * ------------------------- - */ - // TODO implement pre search to directly open the direct group - - public static void launchForKeyboardSelection(Activity activity, boolean readOnly) { - TimeoutHelper.INSTANCE.recordTime(activity); - buildAndLaunchIntent(activity, null, readOnly, - (intent) -> KeyboardHelper.INSTANCE.startActivityForKeyboardSelection(activity, intent)); - } - - /* - * ------------------------- - * Autofill Launch - * ------------------------- - */ - // TODO implement pre search to directly open the direct group - - @RequiresApi(api = Build.VERSION_CODES.O) - public static void launchForAutofillResult(Activity activity, @NonNull AssistStructure assistStructure, boolean readOnly) { - TimeoutHelper.INSTANCE.recordTime(activity); - buildAndLaunchIntent(activity, null, readOnly, - (intent) -> AutofillHelper.INSTANCE.startActivityForAutofillResult(activity, intent, assistStructure)); - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - if (isFinishing()) { - return; - } - - database = App.Companion.getCurrentDatabase(); - - // Construct main view - setContentView(getLayoutInflater().inflate(R.layout.list_nodes_with_add_button, null)); - - // Initialize views - iconView = findViewById(R.id.icon); - addNodeButtonView = findViewById(R.id.add_node_button); - toolbar = findViewById(R.id.toolbar); - searchTitleView = findViewById(R.id.search_title); - groupNameView = findViewById(R.id.group_name); - toolbarPasteExpandableLayout = findViewById(R.id.expandable_toolbar_paste_layout); - toolbarPaste = findViewById(R.id.toolbar_paste); - modeTitleView = findViewById(R.id.mode_title_view); - - // Focus view to reinitialize timeout - resetAppTimeoutWhenViewFocusedOrChanged(addNodeButtonView); - - // Retrieve elements after an orientation change - if (savedInstanceState != null) { - if (savedInstanceState.containsKey(OLD_GROUP_TO_UPDATE_KEY)) - oldGroupToUpdate = savedInstanceState.getParcelable(OLD_GROUP_TO_UPDATE_KEY); - - if (savedInstanceState.containsKey(NODE_TO_COPY_KEY)) { - nodeToCopy = savedInstanceState.getParcelable(NODE_TO_COPY_KEY); - toolbarPaste.setOnMenuItemClickListener(new OnCopyMenuItemClickListener()); - } - else if (savedInstanceState.containsKey(NODE_TO_MOVE_KEY)) { - nodeToMove = savedInstanceState.getParcelable(NODE_TO_MOVE_KEY); - toolbarPaste.setOnMenuItemClickListener(new OnMoveMenuItemClickListener()); - } - } - - try { - rootGroup = database.getRootGroup(); - } catch (NullPointerException e) { - Log.e(TAG, "Unable to get rootGroup"); - } - mCurrentGroup = retrieveCurrentGroup(getIntent(), savedInstanceState); - currentGroupIsASearch = Intent.ACTION_SEARCH.equals(getIntent().getAction()); - - Log.i(TAG, "Started creating tree"); - if ( mCurrentGroup == null ) { - Log.w(TAG, "Group was null"); - return; - } - - // Update last access time. - mCurrentGroup.touch(false, false); - - toolbar.setTitle(""); - setSupportActionBar(toolbar); - - toolbarPaste.inflateMenu(R.menu.node_paste_menu); - toolbarPaste.setNavigationIcon(R.drawable.ic_arrow_left_white_24dp); - toolbarPaste.setNavigationOnClickListener(view -> { - toolbarPasteExpandableLayout.collapse(); - nodeToCopy = null; - nodeToMove = null; - }); - - // Retrieve the textColor to tint the icon - int[] attrs = {R.attr.textColorInverse}; - TypedArray ta = getTheme().obtainStyledAttributes(attrs); - iconColor = ta.getColor(0, Color.WHITE); - - String fragmentTag = LIST_NODES_FRAGMENT_TAG; - if (currentGroupIsASearch) - fragmentTag = SEARCH_FRAGMENT_TAG; - - // Initialize the fragment with the list - listNodesFragment = (ListNodesFragment) getSupportFragmentManager() - .findFragmentByTag(fragmentTag); - if (listNodesFragment == null) - listNodesFragment = ListNodesFragment.newInstance(mCurrentGroup, getReadOnly(), currentGroupIsASearch); - - // Attach fragment to content view - getSupportFragmentManager().beginTransaction().replace( - R.id.nodes_list_fragment_container, - listNodesFragment, - fragmentTag) - .commit(); - - // Add listeners to the add buttons - addNodeButtonView.setAddGroupClickListener(v -> GroupEditDialogFragment.build() - .show(getSupportFragmentManager(), - GroupEditDialogFragment.TAG_CREATE_GROUP)); - addNodeButtonView.setAddEntryClickListener(v -> - EntryEditActivity.Companion.launch(GroupActivity.this, mCurrentGroup)); - - // Search suggestion - searchSuggestionAdapter = new SearchEntryCursorAdapter(this, database); - - Log.i(TAG, "Finished creating tree"); - } - - @Override - protected void onNewIntent(Intent intent) { - Log.d(TAG, "setNewIntent: " + intent.toString()); - setIntent(intent); - if (Intent.ACTION_SEARCH.equals(intent.getAction())) { - // only one instance of search in backstack - openSearchGroup(retrieveCurrentGroup(intent, null)); - currentGroupIsASearch = true; - } else { - currentGroupIsASearch = false; - } - } - - private void openSearchGroup(GroupVersioned group) { - // Delete the previous search fragment - Fragment searchFragment = getSupportFragmentManager().findFragmentByTag(SEARCH_FRAGMENT_TAG); - if (searchFragment != null) { - if ( getSupportFragmentManager() - .popBackStackImmediate(SEARCH_FRAGMENT_TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE) ) - getSupportFragmentManager().beginTransaction().remove(searchFragment).commit(); - } - - openGroup(group, true); - } - - private void openChildGroup(GroupVersioned group) { - openGroup(group, false); - } - - private void openGroup(GroupVersioned group, boolean isASearch) { - // Check TimeoutHelper - TimeoutHelper.INSTANCE.checkTimeAndLockIfTimeoutOrResetTimeout(this, () -> { - // Open a group in a new fragment - ListNodesFragment newListNodeFragment = ListNodesFragment.newInstance(group, getReadOnly(), isASearch); - FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); - // Different animation - String fragmentTag; - if (isASearch) { - fragmentTransaction.setCustomAnimations(R.anim.slide_in_top, R.anim.slide_out_bottom, - R.anim.slide_in_bottom, R.anim.slide_out_top); - fragmentTag = SEARCH_FRAGMENT_TAG; - } else { - fragmentTransaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, - R.anim.slide_in_left, R.anim.slide_out_right); - fragmentTag = LIST_NODES_FRAGMENT_TAG; - } - - fragmentTransaction.replace(R.id.nodes_list_fragment_container, - newListNodeFragment, - fragmentTag); - fragmentTransaction.addToBackStack(fragmentTag); - fragmentTransaction.commit(); - - listNodesFragment = newListNodeFragment; - mCurrentGroup = group; - assignGroupViewElements(); - return null; - }); - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - if (mCurrentGroup != null) - outState.putParcelable(GROUP_ID_KEY, mCurrentGroup.getNodeId()); - outState.putParcelable(OLD_GROUP_TO_UPDATE_KEY, oldGroupToUpdate); - if (nodeToCopy != null) - outState.putParcelable(NODE_TO_COPY_KEY, nodeToCopy); - if (nodeToMove != null) - outState.putParcelable(NODE_TO_MOVE_KEY, nodeToMove); - super.onSaveInstanceState(outState); - } - - protected @Nullable GroupVersioned retrieveCurrentGroup(Intent intent, @Nullable Bundle savedInstanceState) { - - // If it's a search - if ( Intent.ACTION_SEARCH.equals(intent.getAction()) ) { - return database.search(intent.getStringExtra(SearchManager.QUERY).trim()); - } - // else a real group - else { - PwNodeId pwGroupId = null; - if (savedInstanceState != null - && savedInstanceState.containsKey(GROUP_ID_KEY)) { - pwGroupId = savedInstanceState.getParcelable(GROUP_ID_KEY); - } else { - if (getIntent() != null) - pwGroupId = intent.getParcelableExtra(GROUP_ID_KEY); - } - - setReadOnly(database.isReadOnly() || getReadOnly()); // Force read only if the database is like that - - Log.w(TAG, "Creating tree view"); - GroupVersioned currentGroup; - if (pwGroupId == null) { - currentGroup = rootGroup; - } else { - currentGroup = database.getGroupById(pwGroupId); - } - - return currentGroup; - } - } - - public void assignGroupViewElements() { - // Assign title - if (mCurrentGroup != null) { - String title = mCurrentGroup.getTitle(); - if (title != null && title.length() > 0) { - if (groupNameView != null) { - groupNameView.setText(title); - groupNameView.invalidate(); - } - } else { - if (groupNameView != null) { - groupNameView.setText(getText(R.string.root)); - groupNameView.invalidate(); - } - } - } - if (currentGroupIsASearch) { - searchTitleView.setVisibility(View.VISIBLE); - } else { - searchTitleView.setVisibility(View.GONE); - } - - // Assign icon - if (currentGroupIsASearch) { - if (toolbar != null) { - toolbar.setNavigationIcon(null); - } - iconView.setVisibility(View.GONE); - } else { - // Assign the group icon depending of IconPack or custom icon - iconView.setVisibility(View.VISIBLE); - if (mCurrentGroup != null) { - database.getDrawFactory().assignDatabaseIconTo(this, iconView, mCurrentGroup.getIcon(), iconColor); - - if (toolbar != null) { - if (mCurrentGroup.containsParent()) - toolbar.setNavigationIcon(R.drawable.ic_arrow_up_white_24dp); - else { - toolbar.setNavigationIcon(null); - } - } - } - } - - // Show selection mode message if needed - if (getSelectionMode()) { - modeTitleView.setVisibility(View.VISIBLE); - } else { - modeTitleView.setVisibility(View.GONE); - } - - // Show button if allowed - if (addNodeButtonView != null) { - - // To enable add button - boolean addGroupEnabled = !getReadOnly() && !currentGroupIsASearch; - boolean addEntryEnabled = !getReadOnly() && !currentGroupIsASearch; - if (mCurrentGroup != null) { - boolean isRoot = (mCurrentGroup != null && mCurrentGroup == rootGroup); - if (!mCurrentGroup.allowAddEntryIfIsRoot()) - addEntryEnabled = !isRoot && addEntryEnabled; - if (isRoot) { - showWarnings(); - } - } - addNodeButtonView.enableAddGroup(addGroupEnabled); - addNodeButtonView.enableAddEntry(addEntryEnabled); - - if (addNodeButtonView.isEnable()) - addNodeButtonView.showButton(); - } - } - - @Override - public void onScrolled(int dy) { - if (addNodeButtonView != null) - addNodeButtonView.hideButtonOnScrollListener(dy); - } - - @Override - public void onNodeClick(NodeVersioned node) { - switch (node.getType()) { - case GROUP: - try { - openChildGroup((GroupVersioned) node); - } catch (ClassCastException e) { - Log.e(TAG, "Node can't be cast in Group"); - } - break; - case ENTRY: - try { - EntryVersioned entry = ((EntryVersioned) node); - EntrySelectionHelper.INSTANCE.doEntrySelectionAction(getIntent(), - () -> { - EntryActivity.Companion.launch(GroupActivity.this, entry, getReadOnly()); - return null; - }, - () -> { - MagikIME.setEntryKey(getEntry(entry)); - // Show the notification if allowed in Preferences - if (PreferencesUtil.enableKeyboardNotificationEntry(GroupActivity.this)) { - startService(new Intent( - GroupActivity.this, - KeyboardEntryNotificationService.class)); - } - // Consume the selection mode - EntrySelectionHelper.INSTANCE.removeEntrySelectionModeFromIntent(getIntent()); - moveTaskToBack(true); - return null; - }, - assistStructure -> { - // Build response with the entry selected - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - AutofillHelper.INSTANCE.buildResponseWhenEntrySelected(GroupActivity.this, entry); - } - finish(); - return null; - }); - } catch (ClassCastException e) { - Log.e(TAG, "Node can't be cast in Entry"); - } - break; - } - } - - private Entry getEntry(EntryVersioned entry) { - Entry entryModel = new Entry(); - entryModel.setTitle(entry.getTitle()); - entryModel.setUsername(entry.getUsername()); - entryModel.setPassword(entry.getPassword()); - entryModel.setUrl(entry.getUrl()); - if (entry.containsCustomFields()) { - entry.getFields() - .doActionToAllCustomProtectedField(new Function2() { - @Override - public Unit invoke(String key, ProtectedString value) { - entryModel.addCustomField( - new Field(key, value.toString())); - return null; - } - }); - } - return entryModel; - } - - @Override - public boolean onOpenMenuClick(NodeVersioned node) { - onNodeClick(node); - return true; - } - - @Override - public boolean onEditMenuClick(NodeVersioned node) { - switch (node.getType()) { - case GROUP: - oldGroupToUpdate = (GroupVersioned) node; - GroupEditDialogFragment.build(oldGroupToUpdate) - .show(getSupportFragmentManager(), - GroupEditDialogFragment.TAG_CREATE_GROUP); - break; - case ENTRY: - EntryEditActivity.Companion.launch(GroupActivity.this, (EntryVersioned) node); - break; - } - return true; - } - - @Override - public boolean onCopyMenuClick(NodeVersioned node) { - - toolbarPasteExpandableLayout.expand(); - nodeToCopy = node; - toolbarPaste.setOnMenuItemClickListener(new OnCopyMenuItemClickListener()); - return false; - } - - private class OnCopyMenuItemClickListener implements Toolbar.OnMenuItemClickListener{ - - @Override - public boolean onMenuItemClick(MenuItem item) { - toolbarPasteExpandableLayout.collapse(); - - switch (item.getItemId()) { - case R.id.menu_paste: - switch (nodeToCopy.getType()) { - case GROUP: - Log.e(TAG, "Copy not allowed for group"); - break; - case ENTRY: - copyEntry((EntryVersioned) nodeToCopy, mCurrentGroup); - break; - } - nodeToCopy = null; - return true; - } - return true; - } - } - - private void copyEntry(EntryVersioned entryToCopy, GroupVersioned newParent) { - new Thread(new CopyEntryRunnable(this, - App.Companion.getCurrentDatabase(), - entryToCopy, - newParent, - new AfterAddNodeRunnable(), - !getReadOnly()) - ).start(); - } - - @Override - public boolean onMoveMenuClick(NodeVersioned node) { - - toolbarPasteExpandableLayout.expand(); - nodeToMove = node; - toolbarPaste.setOnMenuItemClickListener(new OnMoveMenuItemClickListener()); - return false; - } - - private class OnMoveMenuItemClickListener implements Toolbar.OnMenuItemClickListener{ - - @Override - public boolean onMenuItemClick(MenuItem item) { - toolbarPasteExpandableLayout.collapse(); - - switch (item.getItemId()) { - case R.id.menu_paste: - switch (nodeToMove.getType()) { - case GROUP: - moveGroup((GroupVersioned) nodeToMove, mCurrentGroup); - break; - case ENTRY: - moveEntry((EntryVersioned) nodeToMove, mCurrentGroup); - break; - } - nodeToMove = null; - return true; - } - return true; - } - } - - private void moveGroup(GroupVersioned groupToMove, GroupVersioned newParent) { - new Thread(new MoveGroupRunnable( - this, - App.Companion.getCurrentDatabase(), - groupToMove, - newParent, - new AfterAddNodeRunnable(), - !getReadOnly()) - ).start(); - } - - private void moveEntry(EntryVersioned entryToMove, GroupVersioned newParent) { - new Thread(new MoveEntryRunnable( - this, - App.Companion.getCurrentDatabase(), - entryToMove, - newParent, - new AfterAddNodeRunnable(), - !getReadOnly()) - ).start(); - } - - @Override - public boolean onDeleteMenuClick(NodeVersioned node) { - switch (node.getType()) { - case GROUP: - deleteGroup((GroupVersioned) node); - break; - case ENTRY: - deleteEntry((EntryVersioned) node); - break; - } - return true; - } - - private void deleteGroup(GroupVersioned group) { - //TODO Verify trash recycle bin - new Thread(new DeleteGroupRunnable( - this, - App.Companion.getCurrentDatabase(), - group, - new AfterDeleteNodeRunnable(), - !getReadOnly()) - ).start(); - } - - private void deleteEntry(EntryVersioned entry) { - new Thread(new DeleteEntryRunnable( - this, - App.Companion.getCurrentDatabase(), - entry, - new AfterDeleteNodeRunnable(), - !getReadOnly()) - ).start(); - } - - @Override - protected void onResume() { - super.onResume(); - // Refresh the elements - assignGroupViewElements(); - // Refresh suggestions to change preferences - if (searchSuggestionAdapter != null) - searchSuggestionAdapter.reInit(this); - } - - @Override - protected void onStop() { - super.onStop(); - // Hide button - if (addNodeButtonView != null) - addNodeButtonView.hideButton(); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - - MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.search, menu); - inflater.inflate(R.menu.database_lock, menu); - if (!getReadOnly()) - inflater.inflate(R.menu.database_master_key, menu); - if (!getSelectionMode()) { - inflater.inflate(R.menu.default_menu, menu); - MenuUtil.INSTANCE.contributionMenuInflater(inflater, menu); - } - - // Get the SearchView and set the searchable configuration - SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); - assert searchManager != null; - - MenuItem searchItem = menu.findItem(R.id.menu_search); - SearchView searchView = null; - if (searchItem != null) { - searchView = (SearchView) searchItem.getActionView(); - } - if (searchView != null) { - searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, GroupActivity.class))); - searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default - searchView.setSuggestionsAdapter(searchSuggestionAdapter); - searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() { - @Override - public boolean onSuggestionClick(int position) { - onNodeClick(searchSuggestionAdapter.getEntryFromPosition(position)); - return true; - } - - @Override - public boolean onSuggestionSelect(int position) { - return true; - } - }); - } - - super.onCreateOptionsMenu(menu); - - // Launch education screen - new Handler().post(() -> performedNextEducation(new GroupActivityEducation(this), menu)); - - return true; - } - - private void performedNextEducation(GroupActivityEducation groupActivityEducation, - Menu menu) { - // If no node, show education to add new one - if (listNodesFragment != null - && listNodesFragment.isEmpty() - && addNodeButtonView.isEnable() - && groupActivityEducation.checkAndPerformedAddNodeButtonEducation( - addNodeButtonView, - tapTargetView -> { - addNodeButtonView.openButtonIfClose(); - return null; - }, - tapTargetView -> { - performedNextEducation(groupActivityEducation, menu); - return null; - } - )); - else if (toolbar.findViewById(R.id.menu_search) != null - && groupActivityEducation.checkAndPerformedSearchMenuEducation( - toolbar.findViewById(R.id.menu_search), - tapTargetView -> { - menu.findItem(R.id.menu_search).expandActionView(); - return null; - }, - tapTargetView -> { - performedNextEducation(groupActivityEducation, menu); - return null; - })); - else if (toolbar.findViewById(R.id.menu_sort) != null - && groupActivityEducation.checkAndPerformedSortMenuEducation( - toolbar.findViewById(R.id.menu_sort), - tapTargetView -> { - onOptionsItemSelected(menu.findItem(R.id.menu_sort)); - return null; - }, - tapTargetView -> { - performedNextEducation(groupActivityEducation, menu); - return null; - })); - else if (toolbar.findViewById(R.id.menu_lock) != null - && groupActivityEducation.checkAndPerformedLockMenuEducation( - toolbar.findViewById(R.id.menu_lock), - tapTargetView -> { - onOptionsItemSelected(menu.findItem(R.id.menu_lock)); - return null; - }, - tapTargetView -> { - performedNextEducation(groupActivityEducation, menu); - return null; - })); - } - - @Override - public void startActivity(Intent intent) { - - // Get the intent, verify the action and get the query - if (Intent.ACTION_SEARCH.equals(intent.getAction())) { - String query = intent.getStringExtra(SearchManager.QUERY); - // manually launch the real search activity - final Intent searchIntent = new Intent(getApplicationContext(), GroupActivity.class); - // add query to the Intent Extras - searchIntent.setAction(Intent.ACTION_SEARCH); - searchIntent.putExtra(SearchManager.QUERY, query); - - EntrySelectionHelper.INSTANCE.doEntrySelectionAction(intent, - () -> { - GroupActivity.super.startActivity(intent); - return null; - }, - () -> { - KeyboardHelper.INSTANCE.startActivityForKeyboardSelection( - GroupActivity.this, - searchIntent); - finish(); - return null; - }, - assistStructure -> { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - AutofillHelper.INSTANCE.startActivityForAutofillResult( - GroupActivity.this, - searchIntent, - assistStructure); - } - return null; - }); - } else { - super.startActivity(intent); - } - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - - case android.R.id.home: - onBackPressed(); - return true; - - case R.id.menu_search: - //onSearchRequested(); - return true; - - case R.id.menu_lock: - lockAndExit(); - return true; - - case R.id.menu_change_master_key: - setPassword(); - return true; - default: - // Check the time lock before launching settings - MenuUtil.INSTANCE.onDefaultMenuOptionsItemSelected(this, item, getReadOnly(), true); - return super.onOptionsItemSelected(item); - } - } - - private void setPassword() { - AssignMasterKeyDialogFragment dialog = new AssignMasterKeyDialogFragment(); - dialog.show(getSupportFragmentManager(), "passwordDialog"); - } - - @Override - public void approveEditGroup(GroupEditDialogFragment.EditGroupDialogAction action, - String name, - PwIcon icon) { - Database database = App.Companion.getCurrentDatabase(); - - switch (action) { - case CREATION: - // If group creation - // Build the group - GroupVersioned newGroup = database.createGroup(); - newGroup.setTitle(name); - newGroup.setIcon(icon); - // Not really needed here because added in runnable but safe - newGroup.setParent(mCurrentGroup); - - // If group created save it in the database - new Thread(new AddGroupRunnable(this, - App.Companion.getCurrentDatabase(), - newGroup, - mCurrentGroup, - new AfterAddNodeRunnable(), - !getReadOnly()) - ).start(); - - break; - case UPDATE: - // If update add new elements - if (oldGroupToUpdate != null) { - GroupVersioned updateGroup = new GroupVersioned(oldGroupToUpdate); - updateGroup.setTitle(name); - // TODO custom icon - updateGroup.setIcon(icon); - - if (listNodesFragment != null) - listNodesFragment.removeNode(oldGroupToUpdate); - - // If group updated save it in the database - new Thread(new UpdateGroupRunnable(this, - App.Companion.getCurrentDatabase(), - oldGroupToUpdate, - updateGroup, - new AfterUpdateNodeRunnable(), - !getReadOnly()) - ).start(); - } - - break; - } - } - - class AfterAddNodeRunnable extends AfterActionNodeFinishRunnable { - - @Override - public void onActionNodeFinish(@NotNull ActionNodeValues actionNodeValues) { - runOnUiThread(() -> { - if (actionNodeValues.getSuccess()) { - if (listNodesFragment != null) - listNodesFragment.addNode(actionNodeValues.getNewNode()); - } - }); - } - } - - class AfterUpdateNodeRunnable extends AfterActionNodeFinishRunnable { - - @Override - public void onActionNodeFinish(@NotNull ActionNodeValues actionNodeValues) { - runOnUiThread(() -> { - if (actionNodeValues.getSuccess()) { - if (listNodesFragment != null) - listNodesFragment.updateNode(actionNodeValues.getOldNode(), actionNodeValues.getNewNode()); - } - }); - } - } - - class AfterDeleteNodeRunnable extends AfterActionNodeFinishRunnable { - - @Override - public void onActionNodeFinish(@NotNull ActionNodeValues actionNodeValues) { - runOnUiThread(() -> { - if (actionNodeValues.getSuccess()) { - - if (listNodesFragment != null) - listNodesFragment.removeNode(actionNodeValues.getOldNode()); - - if (actionNodeValues.getOldNode() != null) { - GroupVersioned parent = actionNodeValues.getOldNode().getParent(); - Database database = App.Companion.getCurrentDatabase(); - if (database.isRecycleBinAvailable() && - database.isRecycleBinEnabled()) { - GroupVersioned 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); - } - } - } - } - }); - } - } - - @Override - public void cancelEditGroup(GroupEditDialogFragment.EditGroupDialogAction action, - String name, - PwIcon iconId) { - // Do nothing here - } - - @Override - // For icon in create tree dialog - public void iconPicked(Bundle bundle) { - GroupEditDialogFragment groupEditDialogFragment = - (GroupEditDialogFragment) getSupportFragmentManager() - .findFragmentByTag(GroupEditDialogFragment.TAG_CREATE_GROUP); - if (groupEditDialogFragment != null) { - groupEditDialogFragment.iconPicked(bundle); - } - } - - protected void showWarnings() { - if (App.Companion.getCurrentDatabase().isReadOnly()) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - - if (prefs.getBoolean(getString(R.string.show_read_only_warning), true)) { - Dialog dialog = new ReadOnlyDialog(this); - dialog.show(); - } - } - } - - @Override - public void onAssignKeyDialogPositiveClick( - boolean masterPasswordChecked, String masterPassword, - boolean keyFileChecked, Uri keyFile) { - - Thread taskThread = new Thread(new ProgressDialogRunnable(this, - R.string.saving_database, - progressTaskUpdater -> { - return new AssignPasswordInDatabaseRunnable(GroupActivity.this, - database, - masterPasswordChecked, - masterPassword, - keyFileChecked, - keyFile, - true); // TODO save - } - )); - // Show the progress dialog now or after dialog confirmation - if (database.validatePasswordEncoding(masterPassword)) { - taskThread.start(); - } else { - new PasswordEncodingDialogHelper() - .show(this, (dialog, which) -> taskThread.start()); - } - } - - @Override - public void onAssignKeyDialogNegativeClick( - boolean masterPasswordChecked, String masterPassword, - boolean keyFileChecked, Uri keyFile) { - - } - - @Override - public void onSortSelected(SortNodeEnum sortNodeEnum, boolean ascending, boolean groupsBefore, boolean recycleBinBottom) { - if (listNodesFragment != null) - listNodesFragment.onSortSelected(sortNodeEnum, ascending, groupsBefore, recycleBinBottom); - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - AutofillHelper.INSTANCE.onActivityResultSetResultAndFinish(this, requestCode, resultCode, data); - } - - // Not directly get the entry from intent data but from database - // Is refresh from onResume() - } - - @SuppressLint("RestrictedApi") - @Override - public void startActivityForResult(Intent intent, int requestCode, Bundle options) { - /* - * ACTION_SEARCH automatically forces a new task. This occurs when you open a kdb file in - * another app such as Files or GoogleDrive and then Search for an entry. Here we remove the - * FLAG_ACTIVITY_NEW_TASK flag bit allowing search to open it's activity in the current task. - */ - if (Intent.ACTION_SEARCH.equals(intent.getAction())) { - int flags = intent.getFlags(); - flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK; - intent.setFlags(flags); - } - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - super.startActivityForResult(intent, requestCode, options); - } - } - - private void removeSearchInIntent(Intent intent) { - if (Intent.ACTION_SEARCH.equals(intent.getAction())) { - currentGroupIsASearch = false; - intent.setAction(Intent.ACTION_DEFAULT); - intent.removeExtra(SearchManager.QUERY); - } - } - - @Override - public void onBackPressed() { - - // Normal way when we are not in root - if (rootGroup!= null && !rootGroup.equals(mCurrentGroup)) - super.onBackPressed(); - // Else lock if needed - else { - if (PreferencesUtil.isLockDatabaseWhenBackButtonOnRootClicked(this)) { - App.Companion.getCurrentDatabase().closeAndClear(getApplicationContext()); - super.onBackPressed(); - } else { - moveTaskToBack(true); - } - } - - listNodesFragment = (ListNodesFragment) getSupportFragmentManager().findFragmentByTag(LIST_NODES_FRAGMENT_TAG); - // to refresh fragment - listNodesFragment.rebuildList(); - mCurrentGroup = listNodesFragment.getMainGroup(); - removeSearchInIntent(getIntent()); - assignGroupViewElements(); - } -} diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt new file mode 100644 index 000000000..55bc7e07d --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt @@ -0,0 +1,1051 @@ +/* + * Copyright 2019 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 . + */ +package com.kunzisoft.keepass.activities + +import android.annotation.SuppressLint +import android.app.Activity +import android.app.SearchManager +import android.app.assist.AssistStructure +import android.content.ComponentName +import android.content.Context +import android.content.DialogInterface +import android.content.Intent +import android.graphics.Color +import android.net.Uri +import android.os.Build +import android.os.Bundle +import android.os.Handler +import android.preference.PreferenceManager +import android.support.annotation.RequiresApi +import android.support.v4.app.FragmentManager +import android.support.v7.widget.SearchView +import android.support.v7.widget.Toolbar +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.View +import android.widget.ImageView +import android.widget.TextView + +import com.kunzisoft.keepass.R +import com.kunzisoft.keepass.activities.lock.LockingActivity +import com.kunzisoft.keepass.adapters.NodeAdapter +import com.kunzisoft.keepass.adapters.SearchEntryCursorAdapter +import com.kunzisoft.keepass.app.App +import com.kunzisoft.keepass.autofill.AutofillHelper +import com.kunzisoft.keepass.database.SortNodeEnum +import com.kunzisoft.keepass.database.action.AssignPasswordInDatabaseRunnable +import com.kunzisoft.keepass.database.action.ProgressDialogRunnable +import com.kunzisoft.keepass.database.action.node.ActionNodeValues +import com.kunzisoft.keepass.database.action.node.AddGroupRunnable +import com.kunzisoft.keepass.database.action.node.AfterActionNodeFinishRunnable +import com.kunzisoft.keepass.database.action.node.CopyEntryRunnable +import com.kunzisoft.keepass.database.action.node.DeleteEntryRunnable +import com.kunzisoft.keepass.database.action.node.DeleteGroupRunnable +import com.kunzisoft.keepass.database.action.node.MoveEntryRunnable +import com.kunzisoft.keepass.database.action.node.MoveGroupRunnable +import com.kunzisoft.keepass.database.action.node.UpdateGroupRunnable +import com.kunzisoft.keepass.database.element.* +import com.kunzisoft.keepass.dialogs.AssignMasterKeyDialogFragment +import com.kunzisoft.keepass.dialogs.GroupEditDialogFragment +import com.kunzisoft.keepass.dialogs.IconPickerDialogFragment +import com.kunzisoft.keepass.dialogs.PasswordEncodingDialogHelper +import com.kunzisoft.keepass.dialogs.ReadOnlyDialog +import com.kunzisoft.keepass.dialogs.SortDialogFragment +import com.kunzisoft.keepass.education.GroupActivityEducation +import com.kunzisoft.keepass.magikeyboard.KeyboardEntryNotificationService +import com.kunzisoft.keepass.magikeyboard.KeyboardHelper +import com.kunzisoft.keepass.magikeyboard.MagikIME +import com.kunzisoft.keepass.model.Entry +import com.kunzisoft.keepass.model.Field +import com.kunzisoft.keepass.settings.PreferencesUtil +import com.kunzisoft.keepass.timeout.TimeoutHelper +import com.kunzisoft.keepass.utils.MenuUtil +import com.kunzisoft.keepass.view.AddNodeButtonView + +import net.cachapa.expandablelayout.ExpandableLayout + +class GroupActivity : LockingActivity(), GroupEditDialogFragment.EditGroupListener, IconPickerDialogFragment.IconPickerListener, NodeAdapter.NodeMenuListener, ListNodesFragment.OnScrollListener, AssignMasterKeyDialogFragment.AssignPasswordDialogListener, NodeAdapter.NodeClickCallback, SortDialogFragment.SortSelectionListener { + + // Views + private var toolbar: Toolbar? = null + private var searchTitleView: View? = null + private var toolbarPasteExpandableLayout: ExpandableLayout? = null + private var toolbarPaste: Toolbar? = null + private var iconView: ImageView? = null + private var modeTitleView: TextView? = null + private var addNodeButtonView: AddNodeButtonView? = null + private var groupNameView: TextView? = null + + private var mDatabase: Database? = null + + private var listNodesFragment: ListNodesFragment? = null + private var currentGroupIsASearch: Boolean = false + + // Nodes + private var mRootGroup: GroupVersioned? = null + private var mCurrentGroup: GroupVersioned? = null + private var mOldGroupToUpdate: GroupVersioned? = null + private var nodeToCopy: NodeVersioned? = null + private var nodeToMove: NodeVersioned? = null + + private var searchSuggestionAdapter: SearchEntryCursorAdapter? = null + + private var iconColor: Int = 0 + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + if (isFinishing) { + return + } + mDatabase = App.currentDatabase + + // Construct main view + setContentView(layoutInflater.inflate(R.layout.list_nodes_with_add_button, null)) + + // Initialize views + iconView = findViewById(R.id.icon) + addNodeButtonView = findViewById(R.id.add_node_button) + toolbar = findViewById(R.id.toolbar) + searchTitleView = findViewById(R.id.search_title) + groupNameView = findViewById(R.id.group_name) + toolbarPasteExpandableLayout = findViewById(R.id.expandable_toolbar_paste_layout) + toolbarPaste = findViewById(R.id.toolbar_paste) + modeTitleView = findViewById(R.id.mode_title_view) + + // Focus view to reinitialize timeout + resetAppTimeoutWhenViewFocusedOrChanged(addNodeButtonView) + + // Retrieve elements after an orientation change + if (savedInstanceState != null) { + if (savedInstanceState.containsKey(OLD_GROUP_TO_UPDATE_KEY)) + mOldGroupToUpdate = savedInstanceState.getParcelable(OLD_GROUP_TO_UPDATE_KEY) + if (savedInstanceState.containsKey(NODE_TO_COPY_KEY)) { + nodeToCopy = savedInstanceState.getParcelable(NODE_TO_COPY_KEY) + toolbarPaste?.setOnMenuItemClickListener(OnCopyMenuItemClickListener()) + } else if (savedInstanceState.containsKey(NODE_TO_MOVE_KEY)) { + nodeToMove = savedInstanceState.getParcelable(NODE_TO_MOVE_KEY) + toolbarPaste?.setOnMenuItemClickListener(OnMoveMenuItemClickListener()) + } + } + + try { + mRootGroup = mDatabase?.rootGroup + } catch (e: NullPointerException) { + Log.e(TAG, "Unable to get rootGroup") + } + + mCurrentGroup = retrieveCurrentGroup(intent, savedInstanceState) + currentGroupIsASearch = Intent.ACTION_SEARCH == intent.action + + Log.i(TAG, "Started creating tree") + if (mCurrentGroup == null) { + Log.w(TAG, "Group was null") + return + } + + // Update last access time. + mCurrentGroup?.touch(modified = false, touchParents = false) + + toolbar?.title = "" + setSupportActionBar(toolbar) + + toolbarPaste?.inflateMenu(R.menu.node_paste_menu) + toolbarPaste?.setNavigationIcon(R.drawable.ic_arrow_left_white_24dp) + toolbarPaste?.setNavigationOnClickListener { + toolbarPasteExpandableLayout?.collapse() + nodeToCopy = null + nodeToMove = null + } + + // Retrieve the textColor to tint the icon + iconColor = theme + .obtainStyledAttributes(intArrayOf(R.attr.textColorInverse)) + .getColor(0, Color.WHITE) + + var fragmentTag = LIST_NODES_FRAGMENT_TAG + if (currentGroupIsASearch) + fragmentTag = SEARCH_FRAGMENT_TAG + + // Initialize the fragment with the list + listNodesFragment = supportFragmentManager.findFragmentByTag(fragmentTag) as ListNodesFragment? + if (listNodesFragment == null) + listNodesFragment = ListNodesFragment.newInstance(mCurrentGroup, readOnly, currentGroupIsASearch) + + // Attach fragment to content view + supportFragmentManager.beginTransaction().replace( + R.id.nodes_list_fragment_container, + listNodesFragment, + fragmentTag) + .commit() + + // Add listeners to the add buttons + addNodeButtonView?.setAddGroupClickListener { + GroupEditDialogFragment.build() + .show(supportFragmentManager, + GroupEditDialogFragment.TAG_CREATE_GROUP) + } + mCurrentGroup?.let { currentGroup -> + addNodeButtonView?.setAddEntryClickListener { + EntryEditActivity.launch(this@GroupActivity, currentGroup) + } + } + + // Search suggestion + searchSuggestionAdapter = SearchEntryCursorAdapter(this, mDatabase) + + Log.i(TAG, "Finished creating tree") + } + + override fun onNewIntent(intent: Intent) { + Log.d(TAG, "setNewIntent: $intent") + setIntent(intent) + currentGroupIsASearch = if (Intent.ACTION_SEARCH == intent.action) { + // only one instance of search in backstack + openSearchGroup(retrieveCurrentGroup(intent, null)) + true + } else { + false + } + } + + private fun openSearchGroup(group: GroupVersioned?) { + // Delete the previous search fragment + val searchFragment = supportFragmentManager.findFragmentByTag(SEARCH_FRAGMENT_TAG) + if (searchFragment != null) { + if (supportFragmentManager + .popBackStackImmediate(SEARCH_FRAGMENT_TAG, + FragmentManager.POP_BACK_STACK_INCLUSIVE)) + supportFragmentManager.beginTransaction().remove(searchFragment).commit() + } + openGroup(group, true) + } + + private fun openChildGroup(group: GroupVersioned) { + openGroup(group, false) + } + + private fun openGroup(group: GroupVersioned?, isASearch: Boolean) { + // Check TimeoutHelper + TimeoutHelper.checkTimeAndLockIfTimeoutOrResetTimeout(this) { + // Open a group in a new fragment + val newListNodeFragment = ListNodesFragment.newInstance(group, readOnly, isASearch) + val fragmentTransaction = supportFragmentManager.beginTransaction() + // Different animation + val fragmentTag: String + fragmentTag = if (isASearch) { + fragmentTransaction.setCustomAnimations(R.anim.slide_in_top, R.anim.slide_out_bottom, + R.anim.slide_in_bottom, R.anim.slide_out_top) + SEARCH_FRAGMENT_TAG + } else { + fragmentTransaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, + R.anim.slide_in_left, R.anim.slide_out_right) + LIST_NODES_FRAGMENT_TAG + } + + fragmentTransaction.replace(R.id.nodes_list_fragment_container, + newListNodeFragment, + fragmentTag) + fragmentTransaction.addToBackStack(fragmentTag) + fragmentTransaction.commit() + + listNodesFragment = newListNodeFragment + mCurrentGroup = group + assignGroupViewElements() + } + } + + override fun onSaveInstanceState(outState: Bundle) { + mCurrentGroup?.let { + outState.putParcelable(GROUP_ID_KEY, it.nodeId) + } + mOldGroupToUpdate?.let { + outState.putParcelable(OLD_GROUP_TO_UPDATE_KEY, it) + } + nodeToCopy?.let { + outState.putParcelable(NODE_TO_COPY_KEY, it) + } + nodeToMove?.let { + outState.putParcelable(NODE_TO_MOVE_KEY, it) + } + super.onSaveInstanceState(outState) + } + + private fun retrieveCurrentGroup(intent: Intent, savedInstanceState: Bundle?): GroupVersioned? { + + // If it's a search + if (Intent.ACTION_SEARCH == intent.action) { + return mDatabase?.search(intent.getStringExtra(SearchManager.QUERY).trim { it <= ' ' }) + } + // else a real group + else { + var pwGroupId: PwNodeId<*>? = null + if (savedInstanceState != null && savedInstanceState.containsKey(GROUP_ID_KEY)) { + pwGroupId = savedInstanceState.getParcelable(GROUP_ID_KEY) + } else { + if (getIntent() != null) + pwGroupId = intent.getParcelableExtra(GROUP_ID_KEY) + } + + readOnly = mDatabase?.isReadOnly == true || readOnly // Force read only if the database is like that + + Log.w(TAG, "Creating tree view") + val currentGroup: GroupVersioned? + currentGroup = if (pwGroupId == null) { + mRootGroup + } else { + mDatabase?.getGroupById(pwGroupId) + } + + return currentGroup + } + } + + private fun assignGroupViewElements() { + // Assign title + if (mCurrentGroup != null) { + val title = mCurrentGroup?.title + if (title != null && title.isNotEmpty()) { + if (groupNameView != null) { + groupNameView?.text = title + groupNameView?.invalidate() + } + } else { + if (groupNameView != null) { + groupNameView?.text = getText(R.string.root) + groupNameView?.invalidate() + } + } + } + if (currentGroupIsASearch) { + searchTitleView?.visibility = View.VISIBLE + } else { + searchTitleView?.visibility = View.GONE + } + + // Assign icon + if (currentGroupIsASearch) { + if (toolbar != null) { + toolbar?.navigationIcon = null + } + iconView?.visibility = View.GONE + } else { + // Assign the group icon depending of IconPack or custom icon + iconView?.visibility = View.VISIBLE + mCurrentGroup?.let { + mDatabase?.drawFactory?.assignDatabaseIconTo(this, iconView, it.icon, iconColor) + + if (toolbar != null) { + if (mCurrentGroup?.containsParent() == true) + toolbar?.setNavigationIcon(R.drawable.ic_arrow_up_white_24dp) + else { + toolbar?.navigationIcon = null + } + } + } + } + + // Show selection mode message if needed + if (selectionMode) { + modeTitleView?.visibility = View.VISIBLE + } else { + modeTitleView?.visibility = View.GONE + } + + // Show button if allowed + addNodeButtonView?.apply { + + // To enable add button + val addGroupEnabled = !readOnly && !currentGroupIsASearch + var addEntryEnabled = !readOnly && !currentGroupIsASearch + mCurrentGroup?.let { + val isRoot = it == mRootGroup + if (!it.allowAddEntryIfIsRoot()) + addEntryEnabled = !isRoot && addEntryEnabled + if (isRoot) { + showWarnings() + } + } + enableAddGroup(addGroupEnabled) + enableAddEntry(addEntryEnabled) + + if (isEnable) + showButton() + } + } + + override fun onScrolled(dy: Int) { + addNodeButtonView?.hideButtonOnScrollListener(dy) + } + + override fun onNodeClick(node: NodeVersioned) { + when (node.type) { + Type.GROUP -> try { + openChildGroup(node as GroupVersioned) + } catch (e: ClassCastException) { + Log.e(TAG, "Node can't be cast in Group") + } + + Type.ENTRY -> try { + val entry = node as EntryVersioned + EntrySelectionHelper.doEntrySelectionAction(intent, + { + EntryActivity.launch(this@GroupActivity, entry, readOnly) + }, + { + MagikIME.setEntryKey(getEntry(entry)) + // Show the notification if allowed in Preferences + if (PreferencesUtil.enableKeyboardNotificationEntry(this@GroupActivity)) { + startService(Intent( + this@GroupActivity, + KeyboardEntryNotificationService::class.java)) + } + // Consume the selection mode + EntrySelectionHelper.removeEntrySelectionModeFromIntent(intent) + moveTaskToBack(true) + }, + { + // Build response with the entry selected + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + AutofillHelper.buildResponseWhenEntrySelected(this@GroupActivity, entry) + } + finish() + }) + } catch (e: ClassCastException) { + Log.e(TAG, "Node can't be cast in Entry") + } + } + } + + // TODO Builder + private fun getEntry(entry: EntryVersioned): Entry { + val entryModel = Entry() + entryModel.title = entry.title + entryModel.username = entry.username + entryModel.password = entry.password + entryModel.url = entry.url + if (entry.containsCustomFields()) { + entry.fields + .doActionToAllCustomProtectedField { key, value -> + entryModel.addCustomField( + Field(key, value.toString())) + } + } + return entryModel + } + + override fun onOpenMenuClick(node: NodeVersioned): Boolean { + onNodeClick(node) + return true + } + + override fun onEditMenuClick(node: NodeVersioned): Boolean { + when (node.type) { + Type.GROUP -> { + mOldGroupToUpdate = node as GroupVersioned + GroupEditDialogFragment.build(mOldGroupToUpdate!!) + .show(supportFragmentManager, + GroupEditDialogFragment.TAG_CREATE_GROUP) + } + Type.ENTRY -> EntryEditActivity.launch(this@GroupActivity, node as EntryVersioned) + } + return true + } + + override fun onCopyMenuClick(node: NodeVersioned): Boolean { + toolbarPasteExpandableLayout?.expand() + nodeToCopy = node + toolbarPaste?.setOnMenuItemClickListener(OnCopyMenuItemClickListener()) + return false + } + + private inner class OnCopyMenuItemClickListener : Toolbar.OnMenuItemClickListener { + override fun onMenuItemClick(item: MenuItem): Boolean { + toolbarPasteExpandableLayout?.collapse() + + when (item.itemId) { + R.id.menu_paste -> { + when (nodeToCopy?.type) { + Type.GROUP -> Log.e(TAG, "Copy not allowed for group") + Type.ENTRY -> { + mCurrentGroup?.let { currentGroup -> + copyEntry(nodeToCopy as EntryVersioned, currentGroup) + } + } + } + nodeToCopy = null + return true + } + } + return true + } + } + + private fun copyEntry(entryToCopy: EntryVersioned, newParent: GroupVersioned) { + Thread(CopyEntryRunnable(this, + App.currentDatabase, + entryToCopy, + newParent, + AfterAddNodeRunnable(), + !readOnly) + ).start() + } + + override fun onMoveMenuClick(node: NodeVersioned): Boolean { + toolbarPasteExpandableLayout?.expand() + nodeToMove = node + toolbarPaste?.setOnMenuItemClickListener(OnMoveMenuItemClickListener()) + return false + } + + private inner class OnMoveMenuItemClickListener : Toolbar.OnMenuItemClickListener { + override fun onMenuItemClick(item: MenuItem): Boolean { + toolbarPasteExpandableLayout?.collapse() + + when (item.itemId) { + R.id.menu_paste -> { + when (nodeToMove?.type) { + Type.GROUP -> { + mCurrentGroup?.let { currentGroup -> + moveGroup(nodeToMove as GroupVersioned, currentGroup) + } + } + Type.ENTRY -> { + mCurrentGroup?.let { currentGroup -> + moveEntry(nodeToMove as EntryVersioned, currentGroup) + } + } + } + nodeToMove = null + return true + } + } + return true + } + } + + private fun moveGroup(groupToMove: GroupVersioned, newParent: GroupVersioned) { + Thread(MoveGroupRunnable( + this, + App.currentDatabase, + groupToMove, + newParent, + AfterAddNodeRunnable(), + !readOnly) + ).start() + } + + private fun moveEntry(entryToMove: EntryVersioned, newParent: GroupVersioned) { + Thread(MoveEntryRunnable( + this, + App.currentDatabase, + entryToMove, + newParent, + AfterAddNodeRunnable(), + !readOnly) + ).start() + } + + override fun onDeleteMenuClick(node: NodeVersioned): Boolean { + when (node.type) { + Type.GROUP -> deleteGroup(node as GroupVersioned) + Type.ENTRY -> deleteEntry(node as EntryVersioned) + } + return true + } + + private fun deleteGroup(group: GroupVersioned) { + //TODO Verify trash recycle bin + Thread(DeleteGroupRunnable( + this, + App.currentDatabase, + group, + AfterDeleteNodeRunnable(), + !readOnly) + ).start() + } + + private fun deleteEntry(entry: EntryVersioned) { + Thread(DeleteEntryRunnable( + this, + App.currentDatabase, + entry, + AfterDeleteNodeRunnable(), + !readOnly) + ).start() + } + + override fun onResume() { + super.onResume() + // Refresh the elements + assignGroupViewElements() + // Refresh suggestions to change preferences + searchSuggestionAdapter?.reInit(this) + } + + override fun onStop() { + super.onStop() + // Hide button + addNodeButtonView?.hideButton() + } + + override fun onCreateOptionsMenu(menu: Menu): Boolean { + + val inflater = menuInflater + inflater.inflate(R.menu.search, menu) + inflater.inflate(R.menu.database_lock, menu) + if (!readOnly) + inflater.inflate(R.menu.database_master_key, menu) + if (!selectionMode) { + inflater.inflate(R.menu.default_menu, menu) + MenuUtil.contributionMenuInflater(inflater, menu) + } + + // Get the SearchView and set the searchable configuration + val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager + + menu.findItem(R.id.menu_search)?.let { + val searchView = it.actionView as SearchView? + searchView?.apply { + setSearchableInfo(searchManager.getSearchableInfo( + ComponentName(this@GroupActivity, GroupActivity::class.java))) + setIconifiedByDefault(false) // Do not iconify the widget; expand it by default + suggestionsAdapter = searchSuggestionAdapter + setOnSuggestionListener(object : SearchView.OnSuggestionListener { + override fun onSuggestionClick(position: Int): Boolean { + searchSuggestionAdapter?.let { searchAdapter -> + onNodeClick(searchAdapter.getEntryFromPosition(position)) + } + return true + } + + override fun onSuggestionSelect(position: Int): Boolean { + return true + } + }) + } + } + + super.onCreateOptionsMenu(menu) + + // Launch education screen + Handler().post { performedNextEducation(GroupActivityEducation(this), menu) } + + return true + } + + private fun performedNextEducation(groupActivityEducation: GroupActivityEducation, + menu: Menu) { + // If no node, show education to add new one + if (listNodesFragment != null + && listNodesFragment!!.isEmpty + && addNodeButtonView != null + && addNodeButtonView!!.isEnable + && groupActivityEducation.checkAndPerformedAddNodeButtonEducation( + addNodeButtonView!!, + { + addNodeButtonView?.openButtonIfClose() + }, + { + performedNextEducation(groupActivityEducation, menu) + } + )) + else if (toolbar != null + && toolbar!!.findViewById(R.id.menu_search) != null + && groupActivityEducation.checkAndPerformedSearchMenuEducation( + toolbar!!.findViewById(R.id.menu_search), + { + menu.findItem(R.id.menu_search).expandActionView() + }, + { + performedNextEducation(groupActivityEducation, menu) + })) + else if (toolbar != null + && toolbar!!.findViewById(R.id.menu_sort) != null + && groupActivityEducation.checkAndPerformedSortMenuEducation( + toolbar!!.findViewById(R.id.menu_sort), + { + onOptionsItemSelected(menu.findItem(R.id.menu_sort)) + }, + { + performedNextEducation(groupActivityEducation, menu) + })) + else if (toolbar != null + && toolbar!!.findViewById(R.id.menu_lock) != null + && groupActivityEducation.checkAndPerformedLockMenuEducation( + toolbar!!.findViewById(R.id.menu_lock), + { + onOptionsItemSelected(menu.findItem(R.id.menu_lock)) + }, + { + performedNextEducation(groupActivityEducation, menu) + })) + ; + } + + override fun startActivity(intent: Intent) { + + // Get the intent, verify the action and get the query + if (Intent.ACTION_SEARCH == intent.action) { + val query = intent.getStringExtra(SearchManager.QUERY) + // manually launch the real search activity + val searchIntent = Intent(applicationContext, GroupActivity::class.java) + // add query to the Intent Extras + searchIntent.action = Intent.ACTION_SEARCH + searchIntent.putExtra(SearchManager.QUERY, query) + + EntrySelectionHelper.doEntrySelectionAction(intent, + { + super@GroupActivity.startActivity(intent) + }, + { + KeyboardHelper.startActivityForKeyboardSelection( + this@GroupActivity, + searchIntent) + finish() + }, + { assistStructure -> + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + AutofillHelper.startActivityForAutofillResult( + this@GroupActivity, + searchIntent, + assistStructure) + } + }) + } else { + super.startActivity(intent) + } + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + android.R.id.home -> { + onBackPressed() + return true + } + R.id.menu_search -> + //onSearchRequested(); + return true + R.id.menu_lock -> { + lockAndExit() + return true + } + R.id.menu_change_master_key -> { + setPassword() + return true + } + else -> { + // Check the time lock before launching settings + MenuUtil.onDefaultMenuOptionsItemSelected(this, item, readOnly, true) + return super.onOptionsItemSelected(item) + } + } + } + + private fun setPassword() { + AssignMasterKeyDialogFragment().show(supportFragmentManager, "passwordDialog") + } + + override fun approveEditGroup(action: GroupEditDialogFragment.EditGroupDialogAction, + name: String, + icon: PwIcon) { + val database = App.currentDatabase + + when (action) { + GroupEditDialogFragment.EditGroupDialogAction.CREATION -> { + // If group creation + mCurrentGroup?.let { currentGroup -> + // Build the group + database.createGroup()?.let { newGroup-> + newGroup.title = name + newGroup.icon = icon + // Not really needed here because added in runnable but safe + newGroup.parent = currentGroup + + // If group created save it in the database + Thread(AddGroupRunnable(this, + App.currentDatabase, + newGroup, + currentGroup, + AfterAddNodeRunnable(), + !readOnly) + ).start() + } + } + } + GroupEditDialogFragment.EditGroupDialogAction.UPDATE -> + // If update add new elements + mOldGroupToUpdate?.let { oldGroupToUpdate -> + GroupVersioned(oldGroupToUpdate).let { updateGroup -> + updateGroup.title = name + // TODO custom icon + updateGroup.icon = icon + + listNodesFragment?.removeNode(oldGroupToUpdate) + + // If group updated save it in the database + Thread(UpdateGroupRunnable(this, + App.currentDatabase, + oldGroupToUpdate, + updateGroup, + AfterUpdateNodeRunnable(), + !readOnly) + ).start() + } + } + else -> {} + } + } + + internal inner class AfterAddNodeRunnable : AfterActionNodeFinishRunnable() { + override fun onActionNodeFinish(actionNodeValues: ActionNodeValues) { + runOnUiThread { + if (actionNodeValues.success) { + listNodesFragment?.addNode(actionNodeValues.newNode) + } + } + } + } + + internal inner class AfterUpdateNodeRunnable : AfterActionNodeFinishRunnable() { + override fun onActionNodeFinish(actionNodeValues: ActionNodeValues) { + runOnUiThread { + if (actionNodeValues.success) { + listNodesFragment?.updateNode(actionNodeValues.oldNode, actionNodeValues.newNode) + } + } + } + } + + internal inner class AfterDeleteNodeRunnable : AfterActionNodeFinishRunnable() { + override fun onActionNodeFinish(actionNodeValues: ActionNodeValues) { + runOnUiThread { + if (actionNodeValues.success) { + listNodesFragment?.removeNode(actionNodeValues.oldNode) + + actionNodeValues.oldNode?.let { oldNode -> + val parent = oldNode.parent + val database = App.currentDatabase + if (database.isRecycleBinAvailable && database.isRecycleBinEnabled) { + val recycleBin = database.recycleBin + // Add trash if it doesn't exists + if (parent == recycleBin + && mCurrentGroup != null + && mCurrentGroup!!.parent == null + && mCurrentGroup != recycleBin) { + listNodesFragment?.addNode(parent) + } + } + } + } + } + } + } + + override fun cancelEditGroup(action: GroupEditDialogFragment.EditGroupDialogAction, + name: String, + iconId: PwIcon) { + // Do nothing here + } + + override// For icon in create tree dialog + fun iconPicked(bundle: Bundle) { + (supportFragmentManager + .findFragmentByTag(GroupEditDialogFragment.TAG_CREATE_GROUP) as GroupEditDialogFragment) + .iconPicked(bundle) + } + + private fun showWarnings() { + // TODO Preferences + if (App.currentDatabase.isReadOnly) { + val prefs = PreferenceManager.getDefaultSharedPreferences(this) + if (prefs.getBoolean(getString(R.string.show_read_only_warning), true)) { + ReadOnlyDialog(this).show() + } + } + } + + override fun onAssignKeyDialogPositiveClick( + masterPasswordChecked: Boolean, masterPassword: String?, + keyFileChecked: Boolean, keyFile: Uri?) { + + mDatabase?.let { database -> + val taskThread = Thread(ProgressDialogRunnable(this, + R.string.saving_database + ) { + AssignPasswordInDatabaseRunnable(this@GroupActivity, + database, + masterPasswordChecked, + masterPassword, + keyFileChecked, + keyFile, + true) // TODO save + }) + // Show the progress dialog now or after dialog confirmation + if (database.validatePasswordEncoding(masterPassword!!)) { + taskThread.start() + } else { + PasswordEncodingDialogHelper() + .show(this, DialogInterface.OnClickListener{ _, _ -> taskThread.start() }) + } + } + } + + override fun onAssignKeyDialogNegativeClick( + masterPasswordChecked: Boolean, masterPassword: String?, + keyFileChecked: Boolean, keyFile: Uri?) { + + } + + override fun onSortSelected(sortNodeEnum: SortNodeEnum, ascending: Boolean, groupsBefore: Boolean, recycleBinBottom: Boolean) { + listNodesFragment?.onSortSelected(sortNodeEnum, ascending, groupsBefore, recycleBinBottom) + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + AutofillHelper.onActivityResultSetResultAndFinish(this, requestCode, resultCode, data) + } + + // Not directly get the entry from intent data but from database + // Is refresh from onResume() + } + + @SuppressLint("RestrictedApi") + override fun startActivityForResult(intent: Intent, requestCode: Int, options: Bundle?) { + /* + * ACTION_SEARCH automatically forces a new task. This occurs when you open a kdb file in + * another app such as Files or GoogleDrive and then Search for an entry. Here we remove the + * FLAG_ACTIVITY_NEW_TASK flag bit allowing search to open it's activity in the current task. + */ + if (Intent.ACTION_SEARCH == intent.action) { + var flags = intent.flags + flags = flags and Intent.FLAG_ACTIVITY_NEW_TASK.inv() + intent.flags = flags + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + super.startActivityForResult(intent, requestCode, options) + } + } + + private fun removeSearchInIntent(intent: Intent) { + if (Intent.ACTION_SEARCH == intent.action) { + currentGroupIsASearch = false + intent.action = Intent.ACTION_DEFAULT + intent.removeExtra(SearchManager.QUERY) + } + } + + override fun onBackPressed() { + + // Normal way when we are not in root + if (mRootGroup != null && mRootGroup != mCurrentGroup) + super.onBackPressed() + // Else lock if needed + else { + if (PreferencesUtil.isLockDatabaseWhenBackButtonOnRootClicked(this)) { + App.currentDatabase.closeAndClear(applicationContext) + super.onBackPressed() + } else { + moveTaskToBack(true) + } + } + + listNodesFragment = supportFragmentManager.findFragmentByTag(LIST_NODES_FRAGMENT_TAG) as ListNodesFragment + // to refresh fragment + listNodesFragment?.rebuildList() + mCurrentGroup = listNodesFragment?.mainGroup + removeSearchInIntent(intent) + assignGroupViewElements() + } + + companion object { + + private val TAG = GroupActivity::class.java.name + + private const val GROUP_ID_KEY = "GROUP_ID_KEY" + private const val LIST_NODES_FRAGMENT_TAG = "LIST_NODES_FRAGMENT_TAG" + private const val SEARCH_FRAGMENT_TAG = "SEARCH_FRAGMENT_TAG" + private const val OLD_GROUP_TO_UPDATE_KEY = "OLD_GROUP_TO_UPDATE_KEY" + private const val NODE_TO_COPY_KEY = "NODE_TO_COPY_KEY" + private const val NODE_TO_MOVE_KEY = "NODE_TO_MOVE_KEY" + + private fun buildAndLaunchIntent(activity: Activity, group: GroupVersioned?, readOnly: Boolean, + intentBuildLauncher: (Intent) -> Unit) { + if (TimeoutHelper.checkTimeAndLockIfTimeout(activity)) { + val intent = Intent(activity, GroupActivity::class.java) + if (group != null) { + intent.putExtra(GROUP_ID_KEY, group.nodeId) + } + ReadOnlyHelper.putReadOnlyInIntent(intent, readOnly) + intentBuildLauncher.invoke(intent) + } + } + + /* + * ------------------------- + * Standard Launch + * ------------------------- + */ + + @JvmOverloads + fun launch(activity: Activity, readOnly: Boolean = PreferencesUtil.enableReadOnlyDatabase(activity)) { + launch(activity, null, readOnly) + } + + fun launch(activity: Activity, group: GroupVersioned?, readOnly: Boolean) { + TimeoutHelper.recordTime(activity) + buildAndLaunchIntent(activity, group, readOnly) { intent -> + activity.startActivityForResult(intent, 0) + } + } + + /* + * ------------------------- + * Keyboard Launch + * ------------------------- + */ + // TODO implement pre search to directly open the direct group + + fun launchForKeyboardSelection(activity: Activity, readOnly: Boolean) { + TimeoutHelper.recordTime(activity) + buildAndLaunchIntent(activity, null, readOnly) { intent -> + KeyboardHelper.startActivityForKeyboardSelection(activity, intent) + } + } + + /* + * ------------------------- + * Autofill Launch + * ------------------------- + */ + // TODO implement pre search to directly open the direct group + + @RequiresApi(api = Build.VERSION_CODES.O) + fun launchForAutofillResult(activity: Activity, assistStructure: AssistStructure, readOnly: Boolean) { + TimeoutHelper.recordTime(activity) + buildAndLaunchIntent(activity, null, readOnly) { intent -> + AutofillHelper.startActivityForAutofillResult(activity, intent, assistStructure) + } + } + } +} diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/AssignPasswordInDatabaseRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/AssignPasswordInDatabaseRunnable.kt index 90f0d3326..c15a114d5 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/AssignPasswordInDatabaseRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/AssignPasswordInDatabaseRunnable.kt @@ -24,7 +24,6 @@ import android.net.Uri import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.exception.InvalidKeyFileException import com.kunzisoft.keepass.tasks.ActionRunnable -import com.kunzisoft.keepass.utils.UriUtil import com.kunzisoft.keepass.utils.getUriInputStream import java.io.IOException @@ -35,8 +34,8 @@ class AssignPasswordInDatabaseRunnable @JvmOverloads constructor( masterPassword: String?, withKeyFile: Boolean, keyFile: Uri?, - actionRunnable: ActionRunnable? = null, - save: Boolean) + save: Boolean, + actionRunnable: ActionRunnable? = null) : SaveDatabaseRunnable(ctx, db, actionRunnable, save) { private var mMasterPassword: String? = null diff --git a/app/src/main/java/com/kunzisoft/keepass/fileselect/FileDatabaseSelectActivity.java b/app/src/main/java/com/kunzisoft/keepass/fileselect/FileDatabaseSelectActivity.java index 08007f6ef..345a357dd 100644 --- a/app/src/main/java/com/kunzisoft/keepass/fileselect/FileDatabaseSelectActivity.java +++ b/app/src/main/java/com/kunzisoft/keepass/fileselect/FileDatabaseSelectActivity.java @@ -468,8 +468,8 @@ public class FileDatabaseSelectActivity extends StylishActivity implements masterPassword, keyFileChecked, keyFile, - new LaunchGroupActivityFinish(UriUtil.parseDefaultFile(databaseFilename)), - true // TODO get readonly + true, // TODO get readonly + new LaunchGroupActivityFinish(UriUtil.parseDefaultFile(databaseFilename)) ); }) )).start(); @@ -505,7 +505,7 @@ public class FileDatabaseSelectActivity extends StylishActivity implements fileDatabaseHistory.addDatabaseUri(fileURI); mAdapter.notifyDataSetChanged(); updateFileListVisibility(); - GroupActivity.launch(FileDatabaseSelectActivity.this); + GroupActivity.Companion.launch(FileDatabaseSelectActivity.this); } else { Log.e(TAG, "Unable to open the database"); } diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/IntentBuildLauncher.java b/app/src/main/java/com/kunzisoft/keepass/password/IntentBuildLauncher.java similarity index 73% rename from app/src/main/java/com/kunzisoft/keepass/activities/IntentBuildLauncher.java rename to app/src/main/java/com/kunzisoft/keepass/password/IntentBuildLauncher.java index 0d3e0e4be..aa69659d1 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/IntentBuildLauncher.java +++ b/app/src/main/java/com/kunzisoft/keepass/password/IntentBuildLauncher.java @@ -1,4 +1,4 @@ -package com.kunzisoft.keepass.activities; +package com.kunzisoft.keepass.password; import android.content.Intent; diff --git a/app/src/main/java/com/kunzisoft/keepass/password/PasswordActivity.java b/app/src/main/java/com/kunzisoft/keepass/password/PasswordActivity.java index 685898e4f..5b7a941c3 100644 --- a/app/src/main/java/com/kunzisoft/keepass/password/PasswordActivity.java +++ b/app/src/main/java/com/kunzisoft/keepass/password/PasswordActivity.java @@ -52,7 +52,6 @@ import android.widget.Toast; import com.kunzisoft.keepass.R; import com.kunzisoft.keepass.activities.EntrySelectionHelper; import com.kunzisoft.keepass.activities.GroupActivity; -import com.kunzisoft.keepass.activities.IntentBuildLauncher; import com.kunzisoft.keepass.activities.ReadOnlyHelper; import com.kunzisoft.keepass.activities.lock.LockingActivity; import com.kunzisoft.keepass.app.App; @@ -826,18 +825,18 @@ public class PasswordActivity extends StylishActivity private void launchGroupActivity() { EntrySelectionHelper.INSTANCE.doEntrySelectionAction(getIntent(), () -> { - GroupActivity.launch(PasswordActivity.this, readOnly); + GroupActivity.Companion.launch(PasswordActivity.this, readOnly); return null; }, () -> { - GroupActivity.launchForKeyboardSelection(PasswordActivity.this, readOnly); + GroupActivity.Companion.launchForKeyboardSelection(PasswordActivity.this, readOnly); // Do not keep history finish(); return null; }, assistStructure -> { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - GroupActivity.launchForAutofillResult(PasswordActivity.this, assistStructure, readOnly); + GroupActivity.Companion.launchForAutofillResult(PasswordActivity.this, assistStructure, readOnly); } return null; });