mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-30 13:52:58 -08:00
Ask for new credential with a merge #1221
This commit is contained in:
1 parent
cf4e64d6c5
commit
d3e76bcf21
9 files changed
+174
-19
No files matched your search
@@ -26,6 +26,7 @@ import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.PorterDuff
|
||||
import android.net.Uri
|
||||
import android.os.*
|
||||
import android.util.Log
|
||||
import android.view.Menu
|
||||
@@ -65,6 +66,7 @@ import com.kunzisoft.keepass.database.search.SearchHelper
|
||||
import com.kunzisoft.keepass.database.search.SearchParameters
|
||||
import com.kunzisoft.keepass.education.GroupActivityEducation
|
||||
import com.kunzisoft.keepass.model.GroupInfo
|
||||
import com.kunzisoft.keepass.model.MainCredential
|
||||
import com.kunzisoft.keepass.model.RegisterInfo
|
||||
import com.kunzisoft.keepass.model.SearchInfo
|
||||
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_UPDATE_ENTRY_TASK
|
||||
@@ -89,7 +91,8 @@ class GroupActivity : DatabaseLockActivity(),
|
||||
GroupFragment.NodesActionMenuListener,
|
||||
GroupFragment.OnScrollListener,
|
||||
GroupFragment.GroupRefreshedListener,
|
||||
SortDialogFragment.SortSelectionListener {
|
||||
SortDialogFragment.SortSelectionListener,
|
||||
AskMainCredentialDialogFragment.AskMainCredentialDialogListener {
|
||||
|
||||
// Views
|
||||
private var drawerLayout: DrawerLayout? = null
|
||||
@@ -260,9 +263,7 @@ class GroupActivity : DatabaseLockActivity(),
|
||||
// Manage 'merge from" and "save to"
|
||||
mExternalFileHelper = ExternalFileHelper(this)
|
||||
mExternalFileHelper?.buildOpenDocument { uri ->
|
||||
uri?.let {
|
||||
mergeDatabaseFrom(it)
|
||||
}
|
||||
launchDialogToAskMainCredential(uri)
|
||||
}
|
||||
mExternalFileHelper?.buildCreateDocument("application/x-keepass") { uri ->
|
||||
uri?.let {
|
||||
@@ -1035,6 +1036,11 @@ class GroupActivity : DatabaseLockActivity(),
|
||||
.show(supportFragmentManager, GroupEditDialogFragment.TAG_CREATE_GROUP)
|
||||
}
|
||||
|
||||
private fun launchDialogToAskMainCredential(uri: Uri?) {
|
||||
AskMainCredentialDialogFragment.getInstance(uri)
|
||||
.show(supportFragmentManager, AskMainCredentialDialogFragment.TAG_ASK_MAIN_CREDENTIAL)
|
||||
}
|
||||
|
||||
override fun onCopyMenuClick(
|
||||
database: Database,
|
||||
nodes: List<Node>
|
||||
@@ -1090,6 +1096,16 @@ class GroupActivity : DatabaseLockActivity(),
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onAskMainCredentialDialogPositiveClick(databaseUri: Uri?,
|
||||
mainCredential: MainCredential) {
|
||||
databaseUri?.let {
|
||||
mergeDatabaseFrom(it, mainCredential)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onAskMainCredentialDialogNegativeClick(databaseUri: Uri?,
|
||||
mainCredential: MainCredential) { }
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright 2022 Jeremy Jamet / Kunzisoft.
|
||||
*
|
||||
* This file is part of KeePassDX.
|
||||
*
|
||||
* KeePassDX 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.
|
||||
*
|
||||
* KeePassDX 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 KeePassDX. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.kunzisoft.keepass.activities.dialogs
|
||||
|
||||
import android.app.Dialog
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import com.kunzisoft.keepass.activities.helpers.ExternalFileHelper
|
||||
import com.kunzisoft.keepass.model.MainCredential
|
||||
import com.kunzisoft.keepass.utils.UriUtil
|
||||
import com.kunzisoft.keepass.view.MainCredentialView
|
||||
|
||||
class AskMainCredentialDialogFragment : DatabaseDialogFragment() {
|
||||
|
||||
private var mainCredentialView: MainCredentialView? = null
|
||||
|
||||
private var mListener: AskMainCredentialDialogListener? = null
|
||||
|
||||
private var mExternalFileHelper: ExternalFileHelper? = null
|
||||
|
||||
interface AskMainCredentialDialogListener {
|
||||
fun onAskMainCredentialDialogPositiveClick(databaseUri: Uri?, mainCredential: MainCredential)
|
||||
fun onAskMainCredentialDialogNegativeClick(databaseUri: Uri?, mainCredential: MainCredential)
|
||||
}
|
||||
|
||||
override fun onAttach(activity: Context) {
|
||||
super.onAttach(activity)
|
||||
try {
|
||||
mListener = activity as AskMainCredentialDialogListener
|
||||
} catch (e: ClassCastException) {
|
||||
throw ClassCastException(activity.toString()
|
||||
+ " must implement " + AskMainCredentialDialogListener::class.java.name)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDetach() {
|
||||
mListener = null
|
||||
super.onDetach()
|
||||
}
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
activity?.let { activity ->
|
||||
|
||||
var databaseUri: Uri? = null
|
||||
arguments?.apply {
|
||||
if (containsKey(KEY_ASK_CREDENTIAL_URI))
|
||||
databaseUri = getParcelable(KEY_ASK_CREDENTIAL_URI)
|
||||
}
|
||||
|
||||
val builder = AlertDialog.Builder(activity)
|
||||
|
||||
mainCredentialView = MainCredentialView(activity)
|
||||
databaseUri?.let {
|
||||
builder.setTitle(UriUtil.getFileData(requireContext(), it)?.name)
|
||||
}
|
||||
builder.setView(mainCredentialView)
|
||||
// Add action buttons
|
||||
.setPositiveButton(android.R.string.ok) { _, _ ->
|
||||
mListener?.onAskMainCredentialDialogPositiveClick(
|
||||
databaseUri,
|
||||
retrieveMainCredential()
|
||||
)
|
||||
}
|
||||
.setNegativeButton(android.R.string.cancel) { _, _ ->
|
||||
mListener?.onAskMainCredentialDialogNegativeClick(
|
||||
databaseUri,
|
||||
retrieveMainCredential()
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
mExternalFileHelper = ExternalFileHelper(this)
|
||||
mExternalFileHelper?.buildOpenDocument { uri ->
|
||||
if (uri != null) {
|
||||
mainCredentialView?.populateKeyFileTextView(uri)
|
||||
}
|
||||
}
|
||||
mainCredentialView?.setOpenKeyfileClickListener(mExternalFileHelper)
|
||||
|
||||
return builder.create()
|
||||
}
|
||||
|
||||
return super.onCreateDialog(savedInstanceState)
|
||||
}
|
||||
|
||||
private fun retrieveMainCredential(): MainCredential {
|
||||
return mainCredentialView?.getMainCredential() ?: MainCredential()
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private const val KEY_ASK_CREDENTIAL_URI = "KEY_ASK_CREDENTIAL_URI"
|
||||
const val TAG_ASK_MAIN_CREDENTIAL = "TAG_ASK_MAIN_CREDENTIAL"
|
||||
|
||||
fun getInstance(uri: Uri?): AskMainCredentialDialogFragment {
|
||||
val fragment = AskMainCredentialDialogFragment()
|
||||
val args = Bundle()
|
||||
args.putParcelable(KEY_ASK_CREDENTIAL_URI, uri)
|
||||
fragment.arguments = args
|
||||
return fragment
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -271,8 +271,8 @@ abstract class DatabaseLockActivity : DatabaseModeActivity(),
|
||||
mDatabaseTaskProvider?.startDatabaseMerge()
|
||||
}
|
||||
|
||||
fun mergeDatabaseFrom(uri: Uri) {
|
||||
mDatabaseTaskProvider?.startDatabaseMerge(uri)
|
||||
fun mergeDatabaseFrom(uri: Uri, mainCredential: MainCredential) {
|
||||
mDatabaseTaskProvider?.startDatabaseMerge(uri, mainCredential)
|
||||
}
|
||||
|
||||
fun reloadDatabase() {
|
||||
|
||||
@@ -355,11 +355,15 @@ class DatabaseTaskProvider {
|
||||
, ACTION_DATABASE_LOAD_TASK)
|
||||
}
|
||||
|
||||
fun startDatabaseMerge(fromDatabaseUri: Uri? = null) {
|
||||
fun startDatabaseMerge(fromDatabaseUri: Uri? = null,
|
||||
mainCredential: MainCredential? = null) {
|
||||
start(Bundle().apply {
|
||||
if (fromDatabaseUri != null) {
|
||||
putParcelable(DatabaseTaskNotificationService.DATABASE_URI_KEY, fromDatabaseUri)
|
||||
}
|
||||
if (mainCredential != null) {
|
||||
putParcelable(DatabaseTaskNotificationService.MAIN_CREDENTIAL_KEY, mainCredential)
|
||||
}
|
||||
}
|
||||
, ACTION_DATABASE_MERGE_TASK)
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import android.net.Uri
|
||||
import com.kunzisoft.keepass.database.element.Database
|
||||
import com.kunzisoft.keepass.database.element.binary.BinaryData
|
||||
import com.kunzisoft.keepass.database.exception.LoadDatabaseException
|
||||
import com.kunzisoft.keepass.model.MainCredential
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.tasks.ActionRunnable
|
||||
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
|
||||
@@ -31,6 +32,7 @@ import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
|
||||
class MergeDatabaseRunnable(private val context: Context,
|
||||
private val mDatabase: Database,
|
||||
private val mDatabaseToMergeUri: Uri?,
|
||||
private val mDatabaseToMergeMainCredential: MainCredential?,
|
||||
private val progressTaskUpdater: ProgressTaskUpdater?,
|
||||
private val mLoadDatabaseResult: ((Result) -> Unit)?)
|
||||
: ActionRunnable() {
|
||||
@@ -41,11 +43,14 @@ class MergeDatabaseRunnable(private val context: Context,
|
||||
|
||||
override fun onActionRun() {
|
||||
try {
|
||||
mDatabase.mergeData(mDatabaseToMergeUri, null, context.contentResolver,
|
||||
{ memoryWanted ->
|
||||
BinaryData.canMemoryBeAllocatedInRAM(context, memoryWanted)
|
||||
},
|
||||
progressTaskUpdater)
|
||||
mDatabase.mergeData(mDatabaseToMergeUri,
|
||||
mDatabaseToMergeMainCredential,
|
||||
context.contentResolver,
|
||||
{ memoryWanted ->
|
||||
BinaryData.canMemoryBeAllocatedInRAM(context, memoryWanted)
|
||||
},
|
||||
progressTaskUpdater
|
||||
)
|
||||
} catch (e: LoadDatabaseException) {
|
||||
setError(e)
|
||||
}
|
||||
|
||||
@@ -716,6 +716,8 @@ class Database {
|
||||
databaseToMergeMainCredential.masterPassword,
|
||||
keyFileInputStream,
|
||||
)
|
||||
} else {
|
||||
databaseKDB.masterKey = masterKey
|
||||
}
|
||||
}
|
||||
databaseKDB
|
||||
@@ -729,6 +731,8 @@ class Database {
|
||||
databaseToMergeMainCredential.masterPassword,
|
||||
keyFileInputStream,
|
||||
)
|
||||
} else {
|
||||
databaseKDBX.masterKey = masterKey
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -613,14 +613,19 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
|
||||
|
||||
private fun buildDatabaseMergeActionTask(intent: Intent, database: Database): ActionRunnable {
|
||||
var databaseToMergeUri: Uri? = null
|
||||
var databaseToMergeMainCredential: MainCredential? = null
|
||||
if (intent.hasExtra(DATABASE_URI_KEY)) {
|
||||
databaseToMergeUri = intent.getParcelableExtra(DATABASE_URI_KEY)
|
||||
}
|
||||
if (intent.hasExtra(MAIN_CREDENTIAL_KEY)) {
|
||||
databaseToMergeMainCredential = intent.getParcelableExtra(MAIN_CREDENTIAL_KEY)
|
||||
}
|
||||
|
||||
return MergeDatabaseRunnable(
|
||||
this,
|
||||
database,
|
||||
databaseToMergeUri,
|
||||
databaseToMergeMainCredential,
|
||||
this
|
||||
) { result ->
|
||||
// No need to add each info to reload database
|
||||
|
||||
@@ -30,13 +30,16 @@ import android.view.KeyEvent
|
||||
import android.view.LayoutInflater
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
import android.widget.*
|
||||
import android.widget.CompoundButton
|
||||
import android.widget.EditText
|
||||
import android.widget.FrameLayout
|
||||
import android.widget.TextView
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.activities.helpers.ExternalFileHelper
|
||||
import com.kunzisoft.keepass.activities.helpers.setOpenDocumentClickListener
|
||||
import com.kunzisoft.keepass.model.MainCredential
|
||||
import com.kunzisoft.keepass.model.CredentialStorage
|
||||
import com.kunzisoft.keepass.model.MainCredential
|
||||
|
||||
class MainCredentialView @JvmOverloads constructor(context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
@@ -55,7 +58,7 @@ class MainCredentialView @JvmOverloads constructor(context: Context,
|
||||
|
||||
init {
|
||||
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater?
|
||||
inflater?.inflate(R.layout.view_credentials, this)
|
||||
inflater?.inflate(R.layout.view_main_credentials, this)
|
||||
|
||||
passwordView = findViewById(R.id.password)
|
||||
keyFileSelectionView = findViewById(R.id.keyfile_selection)
|
||||
|
||||
Reference in new issue
Block a user