mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Remove WeakReference to retrieve FileDatabaseHistory
This commit is contained in:
@@ -49,12 +49,13 @@ import com.kunzisoft.keepass.activities.helpers.KeyFileHelper
|
||||
import com.kunzisoft.keepass.activities.stylish.StylishActivity
|
||||
import com.kunzisoft.keepass.adapters.FileDatabaseHistoryAdapter
|
||||
import com.kunzisoft.keepass.adapters.FileInfo
|
||||
import com.kunzisoft.keepass.app.database.FileDatabaseHistory
|
||||
import com.kunzisoft.keepass.app.database.FileDatabaseHistoryEntity
|
||||
import com.kunzisoft.keepass.autofill.AutofillHelper
|
||||
import com.kunzisoft.keepass.database.action.CreateDatabaseRunnable
|
||||
import com.kunzisoft.keepass.database.action.ProgressDialogThread
|
||||
import com.kunzisoft.keepass.database.element.Database
|
||||
import com.kunzisoft.keepass.education.FileDatabaseSelectActivityEducation
|
||||
import com.kunzisoft.keepass.app.database.*
|
||||
import com.kunzisoft.keepass.magikeyboard.KeyboardHelper
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.tasks.ActionRunnable
|
||||
@@ -65,7 +66,6 @@ import kotlinx.android.synthetic.main.activity_file_selection.*
|
||||
import net.cachapa.expandablelayout.ExpandableLayout
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
import java.lang.ref.WeakReference
|
||||
|
||||
class FileDatabaseSelectActivity : StylishActivity(),
|
||||
AssignMasterKeyDialogFragment.AssignPasswordDialogListener,
|
||||
@@ -96,7 +96,7 @@ class FileDatabaseSelectActivity : StylishActivity(),
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
mFileDatabaseHistory = FileDatabaseHistory.getInstance(WeakReference(applicationContext))
|
||||
mFileDatabaseHistory = FileDatabaseHistory.getInstance(applicationContext)
|
||||
|
||||
setContentView(R.layout.activity_file_selection)
|
||||
fileListContainer = findViewById(R.id.container_file_list)
|
||||
|
||||
@@ -31,6 +31,7 @@ import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.preference.PreferenceManager
|
||||
import android.support.annotation.IntegerRes
|
||||
import android.support.annotation.RequiresApi
|
||||
import android.support.design.widget.Snackbar
|
||||
import android.support.v7.widget.Toolbar
|
||||
@@ -44,9 +45,13 @@ import android.view.inputmethod.EditorInfo.IME_ACTION_DONE
|
||||
import android.widget.*
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.activities.dialogs.PasswordEncodingDialogFragment
|
||||
import com.kunzisoft.keepass.activities.helpers.*
|
||||
import com.kunzisoft.keepass.activities.helpers.ClipDataCompat
|
||||
import com.kunzisoft.keepass.activities.helpers.EntrySelectionHelper
|
||||
import com.kunzisoft.keepass.activities.helpers.KeyFileHelper
|
||||
import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper
|
||||
import com.kunzisoft.keepass.activities.lock.LockingActivity
|
||||
import com.kunzisoft.keepass.activities.stylish.StylishActivity
|
||||
import com.kunzisoft.keepass.app.database.FileDatabaseHistory
|
||||
import com.kunzisoft.keepass.autofill.AutofillHelper
|
||||
import com.kunzisoft.keepass.database.action.LoadDatabaseRunnable
|
||||
import com.kunzisoft.keepass.database.action.ProgressDialogThread
|
||||
@@ -59,15 +64,14 @@ import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.tasks.ActionRunnable
|
||||
import com.kunzisoft.keepass.utils.MenuUtil
|
||||
import com.kunzisoft.keepass.utils.UriUtil
|
||||
import com.kunzisoft.keepass.view.asError
|
||||
import com.kunzisoft.keepass.view.FingerPrintInfoView
|
||||
import com.kunzisoft.keepass.view.asError
|
||||
import kotlinx.android.synthetic.main.activity_password.*
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
import java.lang.ref.WeakReference
|
||||
|
||||
class PasswordActivity : StylishActivity(),
|
||||
UriIntentInitTaskCallback {
|
||||
class PasswordActivity : StylishActivity() {
|
||||
|
||||
// Views
|
||||
private var toolbar: Toolbar? = null
|
||||
@@ -171,8 +175,7 @@ class PasswordActivity : StylishActivity(),
|
||||
// For check shutdown
|
||||
super.onResume()
|
||||
|
||||
UriIntentInitTask(WeakReference(this), this, mRememberKeyFile)
|
||||
.execute(intent)
|
||||
initUriFromIntent()
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
@@ -180,7 +183,63 @@ class PasswordActivity : StylishActivity(),
|
||||
super.onSaveInstanceState(outState)
|
||||
}
|
||||
|
||||
override fun onPostInitTask(databaseFileUri: Uri?, keyFileUri: Uri?, errorStringId: Int?) {
|
||||
private fun initUriFromIntent() {
|
||||
|
||||
val databaseUri: Uri?
|
||||
val keyFileUri: Uri?
|
||||
@IntegerRes
|
||||
var errorStringId: Int? = null
|
||||
|
||||
// If is a view intent
|
||||
val action = intent.action
|
||||
if (action != null && action == VIEW_INTENT) {
|
||||
val incoming = intent.data
|
||||
|
||||
databaseUri = incoming
|
||||
keyFileUri = ClipDataCompat.getUriFromIntent(intent, KEY_KEYFILE)
|
||||
|
||||
if (incoming == null) {
|
||||
errorStringId = R.string.error_can_not_handle_uri
|
||||
}
|
||||
else if (incoming.scheme == "file") {
|
||||
// Encapsulate file existance with content scheme
|
||||
|
||||
val fileName = incoming.path
|
||||
|
||||
if (fileName?.isNotEmpty() == true) {
|
||||
// No file name
|
||||
errorStringId = R.string.file_not_found
|
||||
}
|
||||
|
||||
val dbFile = File(fileName)
|
||||
if (!dbFile.exists()) {
|
||||
// File does not exist
|
||||
errorStringId = R.string.file_not_found
|
||||
}
|
||||
} else {
|
||||
errorStringId = R.string.error_can_not_handle_uri
|
||||
}
|
||||
|
||||
} else {
|
||||
databaseUri = UriUtil.parseUriFile(intent.getStringExtra(KEY_FILENAME))
|
||||
keyFileUri = UriUtil.parseUriFile(intent.getStringExtra(KEY_KEYFILE))
|
||||
}
|
||||
|
||||
// Post init uri with KeyFile if needed
|
||||
if (mRememberKeyFile && (keyFileUri == null || keyFileUri.toString().isEmpty())) {
|
||||
// Retrieve KeyFile in a thread
|
||||
databaseUri?.let { databaseUriNotNull ->
|
||||
FileDatabaseHistory.getInstance(applicationContext)
|
||||
.getKeyFileUriByDatabaseUri(databaseUriNotNull) {
|
||||
onPostInitUri(databaseUri, it, errorStringId)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
onPostInitUri(databaseUri, keyFileUri, errorStringId)
|
||||
}
|
||||
}
|
||||
|
||||
private fun onPostInitUri(databaseFileUri: Uri?, keyFileUri: Uri?, errorStringId: Int?) {
|
||||
mDatabaseFileUri = databaseFileUri
|
||||
|
||||
if (errorStringId != null) {
|
||||
@@ -552,15 +611,19 @@ class PasswordActivity : StylishActivity(),
|
||||
|
||||
const val KEY_DEFAULT_FILENAME = "defaultFileName"
|
||||
|
||||
private const val KEY_FILENAME = "fileName"
|
||||
private const val KEY_KEYFILE = "keyFile"
|
||||
private const val VIEW_INTENT = "android.intent.action.VIEW"
|
||||
|
||||
private const val KEY_PASSWORD = "password"
|
||||
private const val KEY_LAUNCH_IMMEDIATELY = "launchImmediately"
|
||||
|
||||
private fun buildAndLaunchIntent(activity: Activity, fileName: String, keyFile: String?,
|
||||
intentBuildLauncher: (Intent) -> Unit) {
|
||||
val intent = Intent(activity, PasswordActivity::class.java)
|
||||
intent.putExtra(UriIntentInitTask.KEY_FILENAME, fileName)
|
||||
intent.putExtra(KEY_FILENAME, fileName)
|
||||
if (keyFile != null)
|
||||
intent.putExtra(UriIntentInitTask.KEY_KEYFILE, keyFile)
|
||||
intent.putExtra(KEY_KEYFILE, keyFile)
|
||||
intentBuildLauncher.invoke(intent)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
package com.kunzisoft.keepass.activities.helpers
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.AsyncTask
|
||||
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.app.database.FileDatabaseHistory
|
||||
import com.kunzisoft.keepass.utils.UriUtil
|
||||
|
||||
import java.io.File
|
||||
import java.lang.ref.WeakReference
|
||||
|
||||
class UriIntentInitTask(private val weakContext: WeakReference<Context>,
|
||||
private val uriIntentInitTaskCallback: UriIntentInitTaskCallback,
|
||||
private val isKeyFileNeeded: Boolean)
|
||||
: AsyncTask<Intent, Void, Int>() {
|
||||
|
||||
private var databaseUri: Uri? = null
|
||||
private var keyFileUri: Uri? = null
|
||||
|
||||
override fun doInBackground(vararg args: Intent): Int? {
|
||||
val intent = args[0]
|
||||
val action = intent.action
|
||||
|
||||
// If is a view intent
|
||||
if (action != null && action == VIEW_INTENT) {
|
||||
val incoming = intent.data
|
||||
databaseUri = incoming
|
||||
keyFileUri = ClipDataCompat.getUriFromIntent(intent, KEY_KEYFILE)
|
||||
|
||||
if (incoming == null) {
|
||||
return R.string.error_can_not_handle_uri
|
||||
} else if (incoming.scheme == "file") {
|
||||
val fileName = incoming.path
|
||||
|
||||
if (fileName?.isNotEmpty() == true) {
|
||||
// No file name
|
||||
return R.string.file_not_found
|
||||
}
|
||||
|
||||
val dbFile = File(fileName)
|
||||
if (!dbFile.exists()) {
|
||||
// File does not exist
|
||||
return R.string.file_not_found
|
||||
}
|
||||
|
||||
return null
|
||||
} else if (incoming.scheme == "content") {
|
||||
return null
|
||||
} else {
|
||||
return R.string.error_can_not_handle_uri
|
||||
}
|
||||
|
||||
} else {
|
||||
databaseUri = UriUtil.parseUriFile(intent.getStringExtra(KEY_FILENAME))
|
||||
keyFileUri = UriUtil.parseUriFile(intent.getStringExtra(KEY_KEYFILE))
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
public override fun onPostExecute(result: Int?) {
|
||||
|
||||
if (isKeyFileNeeded && (keyFileUri == null || keyFileUri!!.toString().isEmpty())) {
|
||||
// Retrieve KeyFile in a thread if needed
|
||||
databaseUri?.let { databaseUriNotNull ->
|
||||
FileDatabaseHistory.getInstance(weakContext)
|
||||
.getKeyFileUriByDatabaseUri(databaseUriNotNull) {
|
||||
uriIntentInitTaskCallback.onPostInitTask(databaseUri, it, result)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
uriIntentInitTaskCallback.onPostInitTask(databaseUri, keyFileUri, result)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val KEY_FILENAME = "fileName"
|
||||
const val KEY_KEYFILE = "keyFile"
|
||||
private const val VIEW_INTENT = "android.intent.action.VIEW"
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.kunzisoft.keepass.activities.helpers
|
||||
|
||||
import android.net.Uri
|
||||
|
||||
interface UriIntentInitTaskCallback {
|
||||
fun onPostInitTask(databaseFileUri: Uri?, keyFileUri: Uri?, errorStringId: Int?)
|
||||
}
|
||||
@@ -24,13 +24,12 @@ import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.AsyncTask
|
||||
import com.kunzisoft.keepass.utils.SingletonHolderParameter
|
||||
import java.lang.ref.WeakReference
|
||||
|
||||
class FileDatabaseHistory(val context: WeakReference<Context>) {
|
||||
class FileDatabaseHistory(applicationContext: Context) {
|
||||
|
||||
private val databaseFileHistoryDao =
|
||||
AppDatabase
|
||||
.getDatabase(context.get()!!)
|
||||
.getDatabase(applicationContext)
|
||||
.databaseFileHistoryDao()
|
||||
|
||||
fun getAll(fileHistoryResultListener: (fileDatabaseHistoryResult: List<FileDatabaseHistoryEntity>?) -> Unit) {
|
||||
@@ -136,5 +135,5 @@ class FileDatabaseHistory(val context: WeakReference<Context>) {
|
||||
}
|
||||
}
|
||||
|
||||
companion object : SingletonHolderParameter<FileDatabaseHistory, WeakReference<Context>>(::FileDatabaseHistory)
|
||||
companion object : SingletonHolderParameter<FileDatabaseHistory, Context>(::FileDatabaseHistory)
|
||||
}
|
||||
|
||||
@@ -119,7 +119,9 @@ class LoadDatabaseRunnable(private val mWeakContext: WeakReference<Context>,
|
||||
if (!mRememberKeyFile) {
|
||||
keyFileUri = null
|
||||
}
|
||||
FileDatabaseHistory.getInstance(mWeakContext).addDatabaseUri(uri, keyFileUri)
|
||||
mWeakContext.get()?.let {
|
||||
FileDatabaseHistory.getInstance(it).addDatabaseUri(uri, keyFileUri)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFinishRun(result: Result) {
|
||||
|
||||
@@ -45,14 +45,13 @@ import com.kunzisoft.keepass.activities.dialogs.UnavailableFeatureDialogFragment
|
||||
import com.kunzisoft.keepass.activities.dialogs.UnderDevelopmentFeatureDialogFragment
|
||||
import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper
|
||||
import com.kunzisoft.keepass.activities.stylish.Stylish
|
||||
import com.kunzisoft.keepass.app.database.FileDatabaseHistory
|
||||
import com.kunzisoft.keepass.database.element.Database
|
||||
import com.kunzisoft.keepass.education.Education
|
||||
import com.kunzisoft.keepass.app.database.FileDatabaseHistory
|
||||
import com.kunzisoft.keepass.fingerprint.FingerPrintHelper
|
||||
import com.kunzisoft.keepass.fingerprint.FingerPrintViewsManager
|
||||
import com.kunzisoft.keepass.icons.IconPackChooser
|
||||
import com.kunzisoft.keepass.settings.preferencedialogfragment.*
|
||||
import java.lang.ref.WeakReference
|
||||
|
||||
class NestedSettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceClickListener {
|
||||
|
||||
@@ -119,7 +118,7 @@ class NestedSettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferen
|
||||
val keyFile = findPreference(getString(R.string.keyfile_key))
|
||||
keyFile.setOnPreferenceChangeListener { _, newValue ->
|
||||
if (!(newValue as Boolean)) {
|
||||
FileDatabaseHistory.getInstance(WeakReference(activity.applicationContext)).deleteAllKeyFiles()
|
||||
FileDatabaseHistory.getInstance(activity.applicationContext).deleteAllKeyFiles()
|
||||
}
|
||||
true
|
||||
}
|
||||
@@ -127,7 +126,7 @@ class NestedSettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferen
|
||||
val recentHistory = findPreference(getString(R.string.recentfile_key))
|
||||
recentHistory.setOnPreferenceChangeListener { _, newValue ->
|
||||
if (!(newValue as Boolean)) {
|
||||
FileDatabaseHistory.getInstance(WeakReference(activity.applicationContext)).deleteAll()
|
||||
FileDatabaseHistory.getInstance(activity.applicationContext).deleteAll()
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user