Fix lock bug

This commit is contained in:
J-Jamet committed 2021-08-18 14:53:41 +02:00
1 parent 0756474d40
commit a0f5ed66e2
8 files changed
+160 -126

No files matched your search

@@ -75,6 +75,7 @@ class EntryActivity : DatabaseLockActivity() {
private var toolbar: Toolbar? = null
private var loadingView: ProgressBar? = null
private var mDatabase: Database? = null
private val mEntryViewModel: EntryViewModel by viewModels()
private var mMainEntryId: NodeId<UUID>? = null
@@ -216,9 +217,14 @@ class EntryActivity : DatabaseLockActivity() {
}
mEntryViewModel.historySelected.observe(this) { historySelected ->
launch(this,
historySelected.nodeId,
historySelected.historyPosition)
mDatabase?.let { database ->
launch(
this,
database,
historySelected.nodeId,
historySelected.historyPosition
)
}
}
}
@@ -233,8 +239,8 @@ class EntryActivity : DatabaseLockActivity() {
override fun onDatabaseRetrieved(database: Database?) {
super.onDatabaseRetrieved(database)
mEntryViewModel.setDatabase(database)
mEntryViewModel.loadEntry(mMainEntryId, mHistoryPosition)
mDatabase = database
mEntryViewModel.loadEntry(mDatabase, mMainEntryId, mHistoryPosition)
// Assign title icon
mIcon?.let { icon ->
@@ -293,7 +299,7 @@ class EntryActivity : DatabaseLockActivity() {
when (requestCode) {
EntryEditActivity.ADD_OR_UPDATE_ENTRY_REQUEST_CODE -> {
// Reload the current id from database
mEntryViewModel.updateEntry()
mEntryViewModel.updateEntry(mDatabase)
}
}
@@ -430,23 +436,38 @@ class EntryActivity : DatabaseLockActivity() {
/**
* Open standard Entry activity
*/
fun launch(activity: Activity, entryId: NodeId<UUID>) {
if (TimeoutHelper.checkTimeAndLockIfTimeout(activity)) {
val intent = Intent(activity, EntryActivity::class.java)
intent.putExtra(KEY_ENTRY, entryId)
activity.startActivityForResult(intent, EntryEditActivity.ADD_OR_UPDATE_ENTRY_REQUEST_CODE)
fun launch(activity: Activity,
database: Database,
entryId: NodeId<UUID>) {
if (database.loaded) {
if (TimeoutHelper.checkTimeAndLockIfTimeout(activity)) {
val intent = Intent(activity, EntryActivity::class.java)
intent.putExtra(KEY_ENTRY, entryId)
activity.startActivityForResult(
intent,
EntryEditActivity.ADD_OR_UPDATE_ENTRY_REQUEST_CODE
)
}
}
}
/**
* Open history Entry activity
*/
fun launch(activity: Activity, entryId: NodeId<UUID>, historyPosition: Int) {
if (TimeoutHelper.checkTimeAndLockIfTimeout(activity)) {
val intent = Intent(activity, EntryActivity::class.java)
intent.putExtra(KEY_ENTRY, entryId)
intent.putExtra(KEY_ENTRY_HISTORY_POSITION, historyPosition)
activity.startActivityForResult(intent, EntryEditActivity.ADD_OR_UPDATE_ENTRY_REQUEST_CODE)
fun launch(activity: Activity,
database: Database,
entryId: NodeId<UUID>,
historyPosition: Int) {
if (database.loaded) {
if (TimeoutHelper.checkTimeAndLockIfTimeout(activity)) {
val intent = Intent(activity, EntryActivity::class.java)
intent.putExtra(KEY_ENTRY, entryId)
intent.putExtra(KEY_ENTRY_HISTORY_POSITION, historyPosition)
activity.startActivityForResult(
intent,
EntryEditActivity.ADD_OR_UPDATE_ENTRY_REQUEST_CODE
)
}
}
}
}
@@ -89,6 +89,7 @@ class GroupActivity : DatabaseLockActivity(),
private var addNodeButtonView: AddNodeButtonView? = null
private var groupNameView: TextView? = null
private var mDatabase: Database? = null
private val mGroupViewModel: GroupViewModel by viewModels()
private val mGroupEditViewModel: GroupEditViewModel by viewModels()
@@ -307,7 +308,7 @@ class GroupActivity : DatabaseLockActivity(),
override fun onDatabaseRetrieved(database: Database?) {
super.onDatabaseRetrieved(database)
mGroupViewModel.setDatabase(database)
mDatabase = database
mGroupEditViewModel.setGroupNamesNotAllowed(database?.groupNamesNotAllowed)
mRecyclingBinEnabled = !mDatabaseReadOnly
@@ -316,10 +317,10 @@ class GroupActivity : DatabaseLockActivity(),
mRootGroup = database?.rootGroup
if (mCurrentGroupState == null) {
mRootGroup?.let { rootGroup ->
mGroupViewModel.loadGroup(rootGroup, 0)
mGroupViewModel.loadGroup(database, rootGroup, 0)
}
} else {
mGroupViewModel.loadGroup(mCurrentGroupState)
mGroupViewModel.loadGroup(database, mCurrentGroupState)
}
// Search suggestion
@@ -438,6 +439,7 @@ class GroupActivity : DatabaseLockActivity(),
val searchString =
intent.getStringExtra(SearchManager.QUERY)?.trim { it <= ' ' } ?: ""
mGroupViewModel.loadGroupFromSearch(
mDatabase,
searchString,
PreferencesUtil.omitBackup(this)
)
@@ -564,7 +566,7 @@ class GroupActivity : DatabaseLockActivity(),
}
}
// Open child group
mGroupViewModel.loadGroup(group, 0)
mGroupViewModel.loadGroup(database, group, 0)
} catch (e: ClassCastException) {
Log.e(TAG, "Node can't be cast in Group")
@@ -574,7 +576,11 @@ class GroupActivity : DatabaseLockActivity(),
val entryVersioned = node as Entry
EntrySelectionHelper.doSpecialAction(intent,
{
EntryActivity.launch(this@GroupActivity, entryVersioned.nodeId)
EntryActivity.launch(
this@GroupActivity,
database,
entryVersioned.nodeId
)
},
{
// Nothing here, a search is simply performed
@@ -1061,7 +1067,7 @@ class GroupActivity : DatabaseLockActivity(),
removeSearch()
// Reload real group
try {
mGroupViewModel.loadGroup(mCurrentGroupState)
mGroupViewModel.loadGroup(mDatabase, mCurrentGroupState)
} catch (e: Exception) {
Log.e(TAG, "Unable to rebuild the list after deletion", e)
}
@@ -1083,7 +1089,7 @@ class GroupActivity : DatabaseLockActivity(),
}
else -> {
// Load the previous group
mGroupViewModel.loadGroup(mPreviousGroupsIds.removeLast())
mGroupViewModel.loadGroup(mDatabase, mPreviousGroupsIds.removeLast())
}
}
}
@@ -1277,8 +1283,12 @@ class GroupActivity : DatabaseLockActivity(),
onLaunchActivitySpecialMode: () -> Unit) {
EntrySelectionHelper.doSpecialAction(activity.intent,
{
GroupActivity.launch(activity,
true)
if (database.loaded) {
GroupActivity.launch(
activity,
true
)
}
},
{ searchInfo ->
SearchHelper.checkAutoSearchInfo(activity,
@@ -1312,17 +1322,23 @@ class GroupActivity : DatabaseLockActivity(),
},
{ searchInfo ->
// Save info used with OTP
if (!database.isReadOnly) {
GroupActivity.launchForSaveResult(activity,
if (database.loaded) {
if (!database.isReadOnly) {
GroupActivity.launchForSaveResult(
activity,
searchInfo,
false)
onLaunchActivitySpecialMode()
} else {
Toast.makeText(activity.applicationContext,
false
)
onLaunchActivitySpecialMode()
} else {
Toast.makeText(
activity.applicationContext,
R.string.autofill_read_only_save,
Toast.LENGTH_LONG)
Toast.LENGTH_LONG
)
.show()
onCancelSpecialMode()
onCancelSpecialMode()
}
}
},
{ searchInfo ->
@@ -248,65 +248,74 @@ open class PasswordActivity : DatabaseModeActivity(), AdvancedUnlockFragment.Bui
super.onDatabaseActionFinished(database, actionTask, result)
when (actionTask) {
ACTION_DATABASE_LOAD_TASK -> {
// Recheck advanced unlock if error
advancedUnlockFragment?.initAdvancedUnlockMode()
// CHeck if database really loaded
if (database.loaded) {
// Recheck advanced unlock if error
advancedUnlockFragment?.initAdvancedUnlockMode()
if (result.isSuccess) {
mDatabaseKeyFileUri = null
clearCredentialsViews(true)
launchGroupActivity(database)
} else {
var resultError = ""
val resultException = result.exception
val resultMessage = result.message
if (result.isSuccess) {
mDatabaseKeyFileUri = null
clearCredentialsViews(true)
launchGroupActivity(database)
} else {
var resultError = ""
val resultException = result.exception
val resultMessage = result.message
if (resultException != null) {
resultError = resultException.getLocalizedMessage(resources)
if (resultException != null) {
resultError = resultException.getLocalizedMessage(resources)
when (resultException) {
is DuplicateUuidDatabaseException -> {
// Relaunch loading if we need to fix UUID
showLoadDatabaseDuplicateUuidMessage {
when (resultException) {
is DuplicateUuidDatabaseException -> {
// Relaunch loading if we need to fix UUID
showLoadDatabaseDuplicateUuidMessage {
var databaseUri: Uri? = null
var mainCredential: MainCredential = MainCredential()
var readOnly = true
var cipherEntity: CipherDatabaseEntity? = null
var databaseUri: Uri? = null
var mainCredential = MainCredential()
var readOnly = true
var cipherEntity: CipherDatabaseEntity? = null
result.data?.let { resultData ->
databaseUri = resultData.getParcelable(DATABASE_URI_KEY)
mainCredential = resultData.getParcelable(MAIN_CREDENTIAL_KEY) ?: mainCredential
readOnly = resultData.getBoolean(READ_ONLY_KEY)
cipherEntity = resultData.getParcelable(CIPHER_ENTITY_KEY)
}
result.data?.let { resultData ->
databaseUri = resultData.getParcelable(DATABASE_URI_KEY)
mainCredential =
resultData.getParcelable(MAIN_CREDENTIAL_KEY)
?: mainCredential
readOnly = resultData.getBoolean(READ_ONLY_KEY)
cipherEntity =
resultData.getParcelable(CIPHER_ENTITY_KEY)
}
databaseUri?.let { databaseFileUri ->
showProgressDialogAndLoadDatabase(
databaseFileUri,
mainCredential,
readOnly,
cipherEntity,
true)
databaseUri?.let { databaseFileUri ->
showProgressDialogAndLoadDatabase(
databaseFileUri,
mainCredential,
readOnly,
cipherEntity,
true
)
}
}
}
}
is FileNotFoundDatabaseException -> {
// Remove this default database inaccessible
if (mDefaultDatabase) {
databaseFileViewModel.removeDefaultDatabase()
is FileNotFoundDatabaseException -> {
// Remove this default database inaccessible
if (mDefaultDatabase) {
databaseFileViewModel.removeDefaultDatabase()
}
}
}
}
}
// Show error message
if (resultMessage != null && resultMessage.isNotEmpty()) {
resultError = "$resultError $resultMessage"
// Show error message
if (resultMessage != null && resultMessage.isNotEmpty()) {
resultError = "$resultError $resultMessage"
}
Log.e(TAG, resultError)
Snackbar.make(
coordinatorLayout,
resultError,
Snackbar.LENGTH_LONG
).asError().show()
}
Log.e(TAG, resultError)
Snackbar.make(coordinatorLayout,
resultError,
Snackbar.LENGTH_LONG).asError().show()
}
}
}
@@ -92,20 +92,22 @@ abstract class DatabaseLockActivity : DatabaseModeActivity(),
super.onDatabaseRetrieved(database)
mDatabase = database
// End activity if database not loaded
if (database == null || !database.loaded) {
finish()
}
// Focus view to reinitialize timeout,
// view is not necessary loaded so retry later in resume
viewToInvalidateTimeout()
?.resetAppTimeoutWhenViewFocusedOrChanged(this, database?.loaded)
database?.let {
// End activity if database not loaded
if (!database.loaded) {
finish()
}
// check timeout
if (mTimeoutEnable) {
if (mLockReceiver == null) {
mLockReceiver = LockReceiver {
mDatabase = null
closeDatabase(database)
if (LOCKING_ACTIVITY_UI_VISIBLE_DURING_LOCK == null)
LOCKING_ACTIVITY_UI_VISIBLE_DURING_LOCK = LOCKING_ACTIVITY_UI_VISIBLE
@@ -62,7 +62,6 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
private var mActionTaskBinder = ActionTaskBinder()
private var mActionTaskListeners = LinkedList<ActionTaskListener>()
private var mActionRunning = false
private var mStopServiceAfterCurrentActionFinished = false
private var mIconId: Int = R.drawable.notification_ic_database_load
private var mTitleId: Int = R.string.database_opened
@@ -287,10 +286,11 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
if (intentAction == ACTION_DATABASE_LOAD_TASK) {
saveDatabaseInfo()
}
val save = !database.isReadOnly
&& (intentAction == ACTION_DATABASE_SAVE
|| intent?.getBooleanExtra(SAVE_DATABASE_KEY, false) == true)
// Save the database info after performing save action
if (!database.isReadOnly
&& (intentAction == ACTION_DATABASE_SAVE
|| intent?.getBooleanExtra(SAVE_DATABASE_KEY, false) == true)) {
if (save) {
database.fileUri?.let {
val newSnapFileDatabaseInfo = SnapFileDatabaseInfo.fromFileDatabaseInfo(
FileDatabaseInfo(applicationContext, it))
@@ -300,9 +300,8 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
}
removeIntentData(intent)
TimeoutHelper.releaseTemporarilyDisableTimeout()
if (mStopServiceAfterCurrentActionFinished)
stopSelf()
else if (TimeoutHelper.checkTimeAndLockIfTimeout(this@DatabaseTaskNotificationService)) {
// Stop service after save if user remove task
if (TimeoutHelper.checkTimeAndLockIfTimeout(this@DatabaseTaskNotificationService)) {
if (!database.loaded) {
stopSelf()
} else {
@@ -495,11 +494,6 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
}
}
override fun onTaskRemoved(rootIntent: Intent?) {
super.onTaskRemoved(rootIntent)
mStopServiceAfterCurrentActionFinished = true
}
private fun buildDatabaseCreateActionTask(intent: Intent, database: Database): ActionRunnable? {
if (intent.hasExtra(DATABASE_URI_KEY)
@@ -44,7 +44,7 @@ abstract class LockNotificationService : NotificationService() {
override fun onTaskRemoved(rootIntent: Intent?) {
if (!TimeoutHelper.temporarilyDisableTimeout) {
stopSelf()
actionOnLock()
}
super.onTaskRemoved(rootIntent)
}
@@ -36,8 +36,6 @@ import java.util.*
class EntryViewModel: ViewModel() {
private var mDatabase: Database? = null
val template : LiveData<Template> get() = _template
private val _template = MutableLiveData<Template>()
@@ -67,15 +65,11 @@ class EntryViewModel: ViewModel() {
val historySelected : LiveData<EntryHistory> get() = _historySelected
private val _historySelected = SingleLiveEvent<EntryHistory>()
fun setDatabase(database: Database?) {
mDatabase = database
}
fun loadEntry(entryId: NodeId<UUID>?, historyPosition: Int) {
fun loadEntry(database: Database?, entryId: NodeId<UUID>?, historyPosition: Int) {
if (entryId != null) {
IOActionTask(
{
mDatabase?.getEntryById(entryId)
database?.getEntryById(entryId)
},
{ mainEntry ->
// Manage current version and history
@@ -92,25 +86,25 @@ class EntryViewModel: ViewModel() {
IOActionTask(
{
val entryTemplate = currentEntry?.let {
mDatabase?.getTemplate(it)
database?.getTemplate(it)
} ?: Template.STANDARD
// To simplify template field visibility
currentEntry?.let { entry ->
// Add mainEntry to check the parent and define the template state
mDatabase?.decodeEntryWithTemplateConfiguration(entry, mainEntry)
database?.decodeEntryWithTemplateConfiguration(entry, mainEntry)
?.let {
// To update current modification time
it.touch(modified = false, touchParents = false)
// Build history info
val entryInfoHistory = it.getHistory().map { entryHistory ->
entryHistory.getEntryInfo(mDatabase)
entryHistory.getEntryInfo(database)
}
EntryInfoHistory(
entryTemplate,
it.getEntryInfo(mDatabase),
it.getEntryInfo(database),
entryInfoHistory
)
}
@@ -129,8 +123,8 @@ class EntryViewModel: ViewModel() {
}
}
fun updateEntry() {
loadEntry(_mainEntryId.value, _historyPosition.value ?: -1)
fun updateEntry(database: Database?) {
loadEntry(database, _mainEntryId.value, _historyPosition.value ?: -1)
}
fun onOtpElementUpdated(optElement: OtpElement?) {
@@ -31,41 +31,38 @@ import com.kunzisoft.keepass.database.element.node.Node
class GroupViewModel: ViewModel() {
private var mDatabase: Database? = null
val group : LiveData<SuperGroup> get() = _group
private val _group = MutableLiveData<SuperGroup>()
val firstPositionVisible : LiveData<Int> get() = _firstPositionVisible
private val _firstPositionVisible = MutableLiveData<Int>()
fun setDatabase(database: Database?) {
this.mDatabase = database
}
fun loadGroup(groupState: GroupActivity.GroupState?) {
fun loadGroup(database: Database?,
groupState: GroupActivity.GroupState?) {
IOActionTask(
{
val groupId = groupState?.groupId
if (groupId != null) {
mDatabase?.getGroupById(groupId)
database?.getGroupById(groupId)
} else {
mDatabase?.rootGroup
database?.rootGroup
}
},
{ group ->
if (group != null) {
_group.value = SuperGroup(group,
mDatabase?.recycleBin == group,
database?.recycleBin == group,
groupState?.firstVisibleItem)
}
}
).execute()
}
fun loadGroup(group: Group, showFromPosition: Int?) {
fun loadGroup(database: Database?,
group: Group,
showFromPosition: Int?) {
_group.value = SuperGroup(group,
mDatabase?.recycleBin == group,
database?.recycleBin == group,
showFromPosition)
}
@@ -73,16 +70,17 @@ class GroupViewModel: ViewModel() {
_firstPositionVisible.value = position
}
fun loadGroupFromSearch(searchQuery: String,
fun loadGroupFromSearch(database: Database?,
searchQuery: String,
omitBackup: Boolean) {
IOActionTask(
{
mDatabase?.createVirtualGroupFromSearch(searchQuery, omitBackup)
database?.createVirtualGroupFromSearch(searchQuery, omitBackup)
},
{ group ->
if (group != null) {
_group.value = SuperGroup(group,
mDatabase?.recycleBin == group,
database?.recycleBin == group,
0)
}
}