Fix database loading

This commit is contained in:
J-Jamet
2021-08-04 18:46:56 +02:00
parent 0ced9c8e26
commit e3198031e3
8 changed files with 197 additions and 197 deletions

View File

@@ -47,10 +47,7 @@ class AutofillLauncherActivity : DatabaseActivity() {
override fun onDatabaseRetrieved(database: Database?) { override fun onDatabaseRetrieved(database: Database?) {
super.onDatabaseRetrieved(database) super.onDatabaseRetrieved(database)
// End activity if database not loaded
if (database?.loaded != true) {
finish()
}
// Retrieve selection mode // Retrieve selection mode
EntrySelectionHelper.retrieveSpecialModeFromIntent(intent).let { specialMode -> EntrySelectionHelper.retrieveSpecialModeFromIntent(intent).let { specialMode ->
when (specialMode) { when (specialMode) {
@@ -63,9 +60,7 @@ class AutofillLauncherActivity : DatabaseActivity() {
} }
SearchInfo.getConcreteWebDomain(this, searchInfo.webDomain) { concreteWebDomain -> SearchInfo.getConcreteWebDomain(this, searchInfo.webDomain) { concreteWebDomain ->
searchInfo.webDomain = concreteWebDomain searchInfo.webDomain = concreteWebDomain
database?.let { database -> launchSelection(database, searchInfo)
launchSelection(database, searchInfo)
}
} }
} }
SpecialMode.REGISTRATION -> { SpecialMode.REGISTRATION -> {
@@ -74,9 +69,7 @@ class AutofillLauncherActivity : DatabaseActivity() {
val searchInfo = SearchInfo(registerInfo?.searchInfo) val searchInfo = SearchInfo(registerInfo?.searchInfo)
SearchInfo.getConcreteWebDomain(this, searchInfo.webDomain) { concreteWebDomain -> SearchInfo.getConcreteWebDomain(this, searchInfo.webDomain) { concreteWebDomain ->
searchInfo.webDomain = concreteWebDomain searchInfo.webDomain = concreteWebDomain
database?.let { database -> launchRegistration(database, searchInfo, registerInfo)
launchRegistration(database, searchInfo, registerInfo)
}
} }
} }
else -> { else -> {
@@ -88,7 +81,7 @@ class AutofillLauncherActivity : DatabaseActivity() {
} }
} }
private fun launchSelection(database: Database, private fun launchSelection(database: Database?,
searchInfo: SearchInfo) { searchInfo: SearchInfo) {
// Pass extra for Autofill (EXTRA_ASSIST_STRUCTURE) // Pass extra for Autofill (EXTRA_ASSIST_STRUCTURE)
val autofillComponent = AutofillHelper.retrieveAutofillComponent(intent) val autofillComponent = AutofillHelper.retrieveAutofillComponent(intent)
@@ -110,13 +103,15 @@ class AutofillLauncherActivity : DatabaseActivity() {
searchInfo, searchInfo,
{ items -> { items ->
// Items found // Items found
AutofillHelper.buildResponseAndSetResult(this, database, items) database?.let {
AutofillHelper.buildResponseAndSetResult(this, database, items)
}
finish() finish()
}, },
{ {
// Show the database UI to select the entry // Show the database UI to select the entry
GroupActivity.launchForAutofillResult(this, GroupActivity.launchForAutofillResult(this,
database.isReadOnly, database?.isReadOnly != false,
autofillComponent, autofillComponent,
searchInfo, searchInfo,
false) false)
@@ -131,7 +126,7 @@ class AutofillLauncherActivity : DatabaseActivity() {
} }
} }
private fun launchRegistration(database: Database, private fun launchRegistration(database: Database?,
searchInfo: SearchInfo, searchInfo: SearchInfo,
registerInfo: RegisterInfo?) { registerInfo: RegisterInfo?) {
if (!KeeAutofillService.autofillAllowedFor(searchInfo.applicationId, if (!KeeAutofillService.autofillAllowedFor(searchInfo.applicationId,
@@ -141,7 +136,7 @@ class AutofillLauncherActivity : DatabaseActivity() {
showBlockRestartMessage() showBlockRestartMessage()
setResult(Activity.RESULT_CANCELED) setResult(Activity.RESULT_CANCELED)
} else { } else {
val readOnly = database.isReadOnly val readOnly = database?.isReadOnly != false
SearchHelper.checkAutoSearchInfo(this, SearchHelper.checkAutoSearchInfo(this,
database, database,
searchInfo, searchInfo,

View File

@@ -265,14 +265,15 @@ class EntryEditActivity : LockingActivity(),
mEntryEditViewModel.setDatabase(database) mEntryEditViewModel.setDatabase(database)
mAllowCustomFields = database?.allowEntryCustomFields() == true mAllowCustomFields = database?.allowEntryCustomFields() == true
mAllowOTP = database?.allowOTP == true mAllowOTP = database?.allowOTP == true
database?.let {
mEntryId?.let { mEntryId?.let {
mEntryEditViewModel.initializeEntryToUpdate(it, mRegisterInfo, mSearchInfo) mEntryEditViewModel.initializeEntryToUpdate(it, mRegisterInfo, mSearchInfo)
mEntryId = null mEntryId = null
} }
mParentId?.let { mParentId?.let {
mEntryEditViewModel.initializeEntryToCreate(it, mRegisterInfo, mSearchInfo) mEntryEditViewModel.initializeEntryToCreate(it, mRegisterInfo, mSearchInfo)
mParentId = null mParentId = null
}
} }
} }

View File

@@ -76,19 +76,13 @@ class EntrySelectionLauncherActivity : DatabaseActivity() {
this.otpString = otpString this.otpString = otpString
} }
// End activity if database not loaded
if (database?.loaded != true) {
finish()
}
SearchInfo.getConcreteWebDomain(this, searchInfo.webDomain) { concreteWebDomain -> SearchInfo.getConcreteWebDomain(this, searchInfo.webDomain) { concreteWebDomain ->
searchInfo.webDomain = concreteWebDomain searchInfo.webDomain = concreteWebDomain
database?.let { database -> launch(database, searchInfo)
launch(database, searchInfo)
}
} }
} }
private fun launch(database: Database, private fun launch(database: Database?,
searchInfo: SearchInfo) { searchInfo: SearchInfo) {
if (!searchInfo.containsOnlyNullValues()) { if (!searchInfo.containsOnlyNullValues()) {
@@ -96,7 +90,7 @@ class EntrySelectionLauncherActivity : DatabaseActivity() {
val searchShareForMagikeyboard = PreferencesUtil.isKeyboardSearchShareEnable(this) val searchShareForMagikeyboard = PreferencesUtil.isKeyboardSearchShareEnable(this)
// If database is open // If database is open
val readOnly = database.isReadOnly val readOnly = database?.isReadOnly != false
SearchHelper.checkAutoSearchInfo(this, SearchHelper.checkAutoSearchInfo(this,
database, database,
searchInfo, searchInfo,

View File

@@ -30,25 +30,23 @@ class MagikeyboardLauncherActivity : DatabaseActivity() {
override fun onDatabaseRetrieved(database: Database?) { override fun onDatabaseRetrieved(database: Database?) {
super.onDatabaseRetrieved(database) super.onDatabaseRetrieved(database)
database?.let { val readOnly = database?.isReadOnly != false
val readOnly = database.isReadOnly SearchHelper.checkAutoSearchInfo(this,
SearchHelper.checkAutoSearchInfo(this, database,
database, null,
null, {
{ // Not called
// Not called // if items found directly returns before calling this activity
// if items found directly returns before calling this activity },
}, {
{ // Select if not found
// Select if not found GroupActivity.launchForKeyboardSelectionResult(this, readOnly)
GroupActivity.launchForKeyboardSelectionResult(this, readOnly) },
}, {
{ // Pass extra to get entry
// Pass extra to get entry FileDatabaseSelectActivity.launchForKeyboardSelectionResult(this)
FileDatabaseSelectActivity.launchForKeyboardSelectionResult(this) }
} )
)
}
finish() finish()
} }
} }

View File

@@ -20,8 +20,7 @@
package com.kunzisoft.keepass.database.action package com.kunzisoft.keepass.database.action
import android.content.* import android.content.*
import android.content.Context.BIND_ABOVE_CLIENT import android.content.Context.*
import android.content.Context.BIND_NOT_FOREGROUND
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.os.IBinder import android.os.IBinder
@@ -219,7 +218,7 @@ class DatabaseTaskProvider(private val activity: FragmentActivity) {
private fun bindService() { private fun bindService() {
initServiceConnection() initServiceConnection()
serviceConnection?.let { serviceConnection?.let {
activity.bindService(intentDatabaseTask, it, BIND_NOT_FOREGROUND or BIND_ABOVE_CLIENT) activity.bindService(intentDatabaseTask, it, BIND_AUTO_CREATE or BIND_NOT_FOREGROUND or BIND_ABOVE_CLIENT)
} }
} }

View File

@@ -92,12 +92,14 @@ class SearchHelper {
* Utility method to perform actions if item is found or not after an auto search in [database] * Utility method to perform actions if item is found or not after an auto search in [database]
*/ */
fun checkAutoSearchInfo(context: Context, fun checkAutoSearchInfo(context: Context,
database: Database, database: Database?,
searchInfo: SearchInfo?, searchInfo: SearchInfo?,
onItemsFound: (items: List<EntryInfo>) -> Unit, onItemsFound: (items: List<EntryInfo>) -> Unit,
onItemNotFound: () -> Unit, onItemNotFound: () -> Unit,
onDatabaseClosed: () -> Unit) { onDatabaseClosed: () -> Unit) {
if (database.loaded && TimeoutHelper.checkTime(context)) { if (database == null || !database.loaded) {
onDatabaseClosed.invoke()
} else if (TimeoutHelper.checkTime(context)) {
var searchWithoutUI = false var searchWithoutUI = false
if (PreferencesUtil.isAutofillAutoSearchEnable(context) if (PreferencesUtil.isAutofillAutoSearchEnable(context)
&& searchInfo != null && searchInfo != null
@@ -118,8 +120,6 @@ class SearchHelper {
if (!searchWithoutUI) { if (!searchWithoutUI) {
onItemNotFound.invoke() onItemNotFound.invoke()
} }
} else {
onDatabaseClosed.invoke()
} }
} }

View File

@@ -54,7 +54,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
override val notificationId: Int = 575 override val notificationId: Int = 575
private lateinit var mDatabase: Database private var mDatabase: Database? = null
private val mainScope = CoroutineScope(Dispatchers.Main) private val mainScope = CoroutineScope(Dispatchers.Main)
@@ -132,7 +132,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
fun checkDatabaseInfo() { fun checkDatabaseInfo() {
try { try {
mDatabase.fileUri?.let { mDatabase?.fileUri?.let {
val previousDatabaseInfo = mSnapFileDatabaseInfo val previousDatabaseInfo = mSnapFileDatabaseInfo
val lastFileDatabaseInfo = SnapFileDatabaseInfo.fromFileDatabaseInfo( val lastFileDatabaseInfo = SnapFileDatabaseInfo.fromFileDatabaseInfo(
FileDatabaseInfo(applicationContext, it)) FileDatabaseInfo(applicationContext, it))
@@ -173,7 +173,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
fun saveDatabaseInfo() { fun saveDatabaseInfo() {
try { try {
mDatabase.fileUri?.let { mDatabase?.fileUri?.let {
mSnapFileDatabaseInfo = SnapFileDatabaseInfo.fromFileDatabaseInfo( mSnapFileDatabaseInfo = SnapFileDatabaseInfo.fromFileDatabaseInfo(
FileDatabaseInfo(applicationContext, it)) FileDatabaseInfo(applicationContext, it))
Log.i(TAG, "Database file saved $mSnapFileDatabaseInfo") Log.i(TAG, "Database file saved $mSnapFileDatabaseInfo")
@@ -187,9 +187,11 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
* Force to call [ActionTaskListener.onStartAction] if the action is still running * Force to call [ActionTaskListener.onStartAction] if the action is still running
*/ */
fun checkAction() { fun checkAction() {
if (mActionRunning) { mDatabase?.let { database ->
mActionTaskListeners.forEach { actionTaskListener -> if (mActionRunning) {
actionTaskListener.onStartAction(mDatabase, mTitleId, mMessageId, mWarningId) mActionTaskListeners.forEach { actionTaskListener ->
actionTaskListener.onStartAction(database, mTitleId, mMessageId, mWarningId)
}
} }
} }
} }
@@ -202,7 +204,8 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId) super.onStartCommand(intent, flags, startId)
mDatabase = Database.getInstance() val database = Database.getInstance()
mDatabase = database
mDatabaseListeners.forEach { listener -> mDatabaseListeners.forEach { listener ->
listener.onDatabaseRetrieved(mDatabase) listener.onDatabaseRetrieved(mDatabase)
} }
@@ -212,26 +215,26 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
val intentAction = intent?.action val intentAction = intent?.action
if (intentAction == null && !mDatabase.loaded) { if (intentAction == null && !database.loaded) {
stopSelf() stopSelf()
} }
val actionRunnable: ActionRunnable? = when (intentAction) { val actionRunnable: ActionRunnable? = when (intentAction) {
ACTION_DATABASE_CREATE_TASK -> buildDatabaseCreateActionTask(intent) ACTION_DATABASE_CREATE_TASK -> buildDatabaseCreateActionTask(intent, database)
ACTION_DATABASE_LOAD_TASK -> buildDatabaseLoadActionTask(intent) ACTION_DATABASE_LOAD_TASK -> buildDatabaseLoadActionTask(intent, database)
ACTION_DATABASE_RELOAD_TASK -> buildDatabaseReloadActionTask() ACTION_DATABASE_RELOAD_TASK -> buildDatabaseReloadActionTask(database)
ACTION_DATABASE_ASSIGN_PASSWORD_TASK -> buildDatabaseAssignPasswordActionTask(intent) ACTION_DATABASE_ASSIGN_PASSWORD_TASK -> buildDatabaseAssignPasswordActionTask(intent, database)
ACTION_DATABASE_CREATE_GROUP_TASK -> buildDatabaseCreateGroupActionTask(intent) ACTION_DATABASE_CREATE_GROUP_TASK -> buildDatabaseCreateGroupActionTask(intent, database)
ACTION_DATABASE_UPDATE_GROUP_TASK -> buildDatabaseUpdateGroupActionTask(intent) ACTION_DATABASE_UPDATE_GROUP_TASK -> buildDatabaseUpdateGroupActionTask(intent, database)
ACTION_DATABASE_CREATE_ENTRY_TASK -> buildDatabaseCreateEntryActionTask(intent) ACTION_DATABASE_CREATE_ENTRY_TASK -> buildDatabaseCreateEntryActionTask(intent, database)
ACTION_DATABASE_UPDATE_ENTRY_TASK -> buildDatabaseUpdateEntryActionTask(intent) ACTION_DATABASE_UPDATE_ENTRY_TASK -> buildDatabaseUpdateEntryActionTask(intent, database)
ACTION_DATABASE_COPY_NODES_TASK -> buildDatabaseCopyNodesActionTask(intent) ACTION_DATABASE_COPY_NODES_TASK -> buildDatabaseCopyNodesActionTask(intent, database)
ACTION_DATABASE_MOVE_NODES_TASK -> buildDatabaseMoveNodesActionTask(intent) ACTION_DATABASE_MOVE_NODES_TASK -> buildDatabaseMoveNodesActionTask(intent, database)
ACTION_DATABASE_DELETE_NODES_TASK -> buildDatabaseDeleteNodesActionTask(intent) ACTION_DATABASE_DELETE_NODES_TASK -> buildDatabaseDeleteNodesActionTask(intent, database)
ACTION_DATABASE_RESTORE_ENTRY_HISTORY -> buildDatabaseRestoreEntryHistoryActionTask(intent) ACTION_DATABASE_RESTORE_ENTRY_HISTORY -> buildDatabaseRestoreEntryHistoryActionTask(intent, database)
ACTION_DATABASE_DELETE_ENTRY_HISTORY -> buildDatabaseDeleteEntryHistoryActionTask(intent) ACTION_DATABASE_DELETE_ENTRY_HISTORY -> buildDatabaseDeleteEntryHistoryActionTask(intent, database)
ACTION_DATABASE_UPDATE_COMPRESSION_TASK -> buildDatabaseUpdateCompressionActionTask(intent) ACTION_DATABASE_UPDATE_COMPRESSION_TASK -> buildDatabaseUpdateCompressionActionTask(intent, database)
ACTION_DATABASE_REMOVE_UNLINKED_DATA_TASK -> buildDatabaseRemoveUnlinkedDataActionTask(intent) ACTION_DATABASE_REMOVE_UNLINKED_DATA_TASK -> buildDatabaseRemoveUnlinkedDataActionTask(intent, database)
ACTION_DATABASE_UPDATE_NAME_TASK, ACTION_DATABASE_UPDATE_NAME_TASK,
ACTION_DATABASE_UPDATE_DESCRIPTION_TASK, ACTION_DATABASE_UPDATE_DESCRIPTION_TASK,
ACTION_DATABASE_UPDATE_DEFAULT_USERNAME_TASK, ACTION_DATABASE_UPDATE_DEFAULT_USERNAME_TASK,
@@ -244,8 +247,8 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
ACTION_DATABASE_UPDATE_KEY_DERIVATION_TASK, ACTION_DATABASE_UPDATE_KEY_DERIVATION_TASK,
ACTION_DATABASE_UPDATE_MEMORY_USAGE_TASK, ACTION_DATABASE_UPDATE_MEMORY_USAGE_TASK,
ACTION_DATABASE_UPDATE_PARALLELISM_TASK, ACTION_DATABASE_UPDATE_PARALLELISM_TASK,
ACTION_DATABASE_UPDATE_ITERATIONS_TASK -> buildDatabaseUpdateElementActionTask(intent) ACTION_DATABASE_UPDATE_ITERATIONS_TASK -> buildDatabaseUpdateElementActionTask(intent, database)
ACTION_DATABASE_SAVE -> buildDatabaseSave(intent) ACTION_DATABASE_SAVE -> buildDatabaseSave(intent, database)
else -> null else -> null
} }
@@ -265,7 +268,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
}) })
mActionTaskListeners.forEach { actionTaskListener -> mActionTaskListeners.forEach { actionTaskListener ->
actionTaskListener.onStartAction(mDatabase, mTitleId, mMessageId, mWarningId) actionTaskListener.onStartAction(database, mTitleId, mMessageId, mWarningId)
} }
}, },
@@ -275,7 +278,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
{ result -> { result ->
try { try {
mActionTaskListeners.forEach { actionTaskListener -> mActionTaskListeners.forEach { actionTaskListener ->
actionTaskListener.onStopAction(mDatabase, intentAction!!, result) actionTaskListener.onStopAction(database, intentAction!!, result)
} }
} finally { } finally {
// Save the database info before performing action // Save the database info before performing action
@@ -285,7 +288,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
// Save the database info after performing save action // Save the database info after performing save action
if (intentAction == ACTION_DATABASE_SAVE if (intentAction == ACTION_DATABASE_SAVE
|| intent?.getBooleanExtra(SAVE_DATABASE_KEY, false) == true) { || intent?.getBooleanExtra(SAVE_DATABASE_KEY, false) == true) {
mDatabase.fileUri?.let { database.fileUri?.let {
val newSnapFileDatabaseInfo = SnapFileDatabaseInfo.fromFileDatabaseInfo( val newSnapFileDatabaseInfo = SnapFileDatabaseInfo.fromFileDatabaseInfo(
FileDatabaseInfo(applicationContext, it)) FileDatabaseInfo(applicationContext, it))
mLastLocalSaveTime = System.currentTimeMillis() mLastLocalSaveTime = System.currentTimeMillis()
@@ -295,7 +298,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
removeIntentData(intent) removeIntentData(intent)
TimeoutHelper.releaseTemporarilyDisableTimeout() TimeoutHelper.releaseTemporarilyDisableTimeout()
if (TimeoutHelper.checkTimeAndLockIfTimeout(this@DatabaseTaskNotificationService)) { if (TimeoutHelper.checkTimeAndLockIfTimeout(this@DatabaseTaskNotificationService)) {
if (!mDatabase.loaded) { if (!database.loaded) {
stopSelf() stopSelf()
} else { } else {
// Restart the service to open lock notification // Restart the service to open lock notification
@@ -386,25 +389,33 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
} }
if (intentAction == null) { if (intentAction == null) {
// Database is normally open mDatabase?.let { database ->
if (mDatabase.loaded) { // Database is normally open
// Build Intents for notification action if (database.loaded) {
val pendingDatabaseIntent = PendingIntent.getActivity(this, // Build Intents for notification action
val pendingDatabaseIntent = PendingIntent.getActivity(
this,
0, 0,
Intent(this, GroupActivity::class.java).apply { Intent(this, GroupActivity::class.java).apply {
ReadOnlyHelper.putReadOnlyInIntent(this, mDatabase.isReadOnly) ReadOnlyHelper.putReadOnlyInIntent(this, database.isReadOnly)
}, },
PendingIntent.FLAG_UPDATE_CURRENT) PendingIntent.FLAG_UPDATE_CURRENT
val pendingDeleteIntent = PendingIntent.getBroadcast(this, )
4576, Intent(LOCK_ACTION), 0) val pendingDeleteIntent = PendingIntent.getBroadcast(
// Add actions in notifications this,
notificationBuilder.apply { 4576, Intent(LOCK_ACTION), 0
setContentText(mDatabase.name + " (" + mDatabase.version + ")") )
setContentIntent(pendingDatabaseIntent) // Add actions in notifications
// Unfortunately swipe is disabled in lollipop+ notificationBuilder.apply {
setDeleteIntent(pendingDeleteIntent) setContentText(database.name + " (" + database.version + ")")
addAction(R.drawable.ic_lock_white_24dp, getString(R.string.lock), setContentIntent(pendingDatabaseIntent)
pendingDeleteIntent) // Unfortunately swipe is disabled in lollipop+
setDeleteIntent(pendingDeleteIntent)
addAction(
R.drawable.ic_lock_white_24dp, getString(R.string.lock),
pendingDeleteIntent
)
}
} }
} }
} }
@@ -464,8 +475,10 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
override fun updateMessage(resId: Int) { override fun updateMessage(resId: Int) {
mMessageId = resId mMessageId = resId
mActionTaskListeners.forEach { actionTaskListener -> mDatabase?.let { database ->
actionTaskListener.onUpdateAction(mDatabase, mTitleId, mMessageId, mWarningId) mActionTaskListeners.forEach { actionTaskListener ->
actionTaskListener.onUpdateAction(database, mTitleId, mMessageId, mWarningId)
}
} }
} }
@@ -479,7 +492,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
} }
} }
private fun buildDatabaseCreateActionTask(intent: Intent): ActionRunnable? { private fun buildDatabaseCreateActionTask(intent: Intent, database: Database): ActionRunnable? {
if (intent.hasExtra(DATABASE_URI_KEY) if (intent.hasExtra(DATABASE_URI_KEY)
&& intent.hasExtra(MAIN_CREDENTIAL_KEY) && intent.hasExtra(MAIN_CREDENTIAL_KEY)
@@ -491,12 +504,12 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
return null return null
return CreateDatabaseRunnable(this, return CreateDatabaseRunnable(this,
mDatabase, database,
databaseUri, databaseUri,
getString(R.string.database_default_name), getString(R.string.database_default_name),
getString(R.string.database), getString(R.string.database),
getString(R.string.template_group_name), getString(R.string.template_group_name),
mainCredential mainCredential
) { result -> ) { result ->
result.data = Bundle().apply { result.data = Bundle().apply {
putParcelable(DATABASE_URI_KEY, databaseUri) putParcelable(DATABASE_URI_KEY, databaseUri)
@@ -508,7 +521,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
} }
} }
private fun buildDatabaseLoadActionTask(intent: Intent): ActionRunnable? { private fun buildDatabaseLoadActionTask(intent: Intent, database: Database): ActionRunnable? {
if (intent.hasExtra(DATABASE_URI_KEY) if (intent.hasExtra(DATABASE_URI_KEY)
&& intent.hasExtra(MAIN_CREDENTIAL_KEY) && intent.hasExtra(MAIN_CREDENTIAL_KEY)
@@ -526,7 +539,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
return LoadDatabaseRunnable( return LoadDatabaseRunnable(
this, this,
mDatabase, database,
databaseUri, databaseUri,
mainCredential, mainCredential,
readOnly, readOnly,
@@ -547,10 +560,10 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
} }
} }
private fun buildDatabaseReloadActionTask(): ActionRunnable { private fun buildDatabaseReloadActionTask(database: Database): ActionRunnable {
return ReloadDatabaseRunnable( return ReloadDatabaseRunnable(
this, this,
mDatabase, database,
this this
) { result -> ) { result ->
// No need to add each info to reload database // No need to add each info to reload database
@@ -558,15 +571,15 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
} }
} }
private fun buildDatabaseAssignPasswordActionTask(intent: Intent): ActionRunnable? { private fun buildDatabaseAssignPasswordActionTask(intent: Intent, database: Database): ActionRunnable? {
return if (intent.hasExtra(DATABASE_URI_KEY) return if (intent.hasExtra(DATABASE_URI_KEY)
&& intent.hasExtra(MAIN_CREDENTIAL_KEY) && intent.hasExtra(MAIN_CREDENTIAL_KEY)
) { ) {
val databaseUri: Uri = intent.getParcelableExtra(DATABASE_URI_KEY) ?: return null val databaseUri: Uri = intent.getParcelableExtra(DATABASE_URI_KEY) ?: return null
AssignPasswordInDatabaseRunnable(this, AssignPasswordInDatabaseRunnable(this,
mDatabase, database,
databaseUri, databaseUri,
intent.getParcelableExtra(MAIN_CREDENTIAL_KEY) ?: MainCredential() intent.getParcelableExtra(MAIN_CREDENTIAL_KEY) ?: MainCredential()
) )
} else { } else {
null null
@@ -583,7 +596,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
} }
} }
private fun buildDatabaseCreateGroupActionTask(intent: Intent): ActionRunnable? { private fun buildDatabaseCreateGroupActionTask(intent: Intent, database: Database): ActionRunnable? {
return if (intent.hasExtra(GROUP_KEY) return if (intent.hasExtra(GROUP_KEY)
&& intent.hasExtra(PARENT_ID_KEY) && intent.hasExtra(PARENT_ID_KEY)
&& intent.hasExtra(SAVE_DATABASE_KEY) && intent.hasExtra(SAVE_DATABASE_KEY)
@@ -595,20 +608,20 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
|| newGroup == null) || newGroup == null)
return null return null
mDatabase.getGroupById(parentId)?.let { parent -> database.getGroupById(parentId)?.let { parent ->
AddGroupRunnable(this, AddGroupRunnable(this,
mDatabase, database,
newGroup, newGroup,
parent, parent,
intent.getBooleanExtra(SAVE_DATABASE_KEY, false), intent.getBooleanExtra(SAVE_DATABASE_KEY, false),
AfterActionNodesRunnable()) AfterActionNodesRunnable())
} }
} else { } else {
null null
} }
} }
private fun buildDatabaseUpdateGroupActionTask(intent: Intent): ActionRunnable? { private fun buildDatabaseUpdateGroupActionTask(intent: Intent, database: Database): ActionRunnable? {
return if (intent.hasExtra(GROUP_ID_KEY) return if (intent.hasExtra(GROUP_ID_KEY)
&& intent.hasExtra(GROUP_KEY) && intent.hasExtra(GROUP_KEY)
&& intent.hasExtra(SAVE_DATABASE_KEY) && intent.hasExtra(SAVE_DATABASE_KEY)
@@ -620,20 +633,20 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
|| newGroup == null) || newGroup == null)
return null return null
mDatabase.getGroupById(groupId)?.let { oldGroup -> database.getGroupById(groupId)?.let { oldGroup ->
UpdateGroupRunnable(this, UpdateGroupRunnable(this,
mDatabase, database,
oldGroup, oldGroup,
newGroup, newGroup,
intent.getBooleanExtra(SAVE_DATABASE_KEY, false), intent.getBooleanExtra(SAVE_DATABASE_KEY, false),
AfterActionNodesRunnable()) AfterActionNodesRunnable())
} }
} else { } else {
null null
} }
} }
private fun buildDatabaseCreateEntryActionTask(intent: Intent): ActionRunnable? { private fun buildDatabaseCreateEntryActionTask(intent: Intent, database: Database): ActionRunnable? {
return if (intent.hasExtra(ENTRY_KEY) return if (intent.hasExtra(ENTRY_KEY)
&& intent.hasExtra(PARENT_ID_KEY) && intent.hasExtra(PARENT_ID_KEY)
&& intent.hasExtra(SAVE_DATABASE_KEY) && intent.hasExtra(SAVE_DATABASE_KEY)
@@ -645,20 +658,20 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
|| newEntry == null) || newEntry == null)
return null return null
mDatabase.getGroupById(parentId)?.let { parent -> database.getGroupById(parentId)?.let { parent ->
AddEntryRunnable(this, AddEntryRunnable(this,
mDatabase, database,
newEntry, newEntry,
parent, parent,
intent.getBooleanExtra(SAVE_DATABASE_KEY, false), intent.getBooleanExtra(SAVE_DATABASE_KEY, false),
AfterActionNodesRunnable()) AfterActionNodesRunnable())
} }
} else { } else {
null null
} }
} }
private fun buildDatabaseUpdateEntryActionTask(intent: Intent): ActionRunnable? { private fun buildDatabaseUpdateEntryActionTask(intent: Intent, database: Database): ActionRunnable? {
return if (intent.hasExtra(ENTRY_ID_KEY) return if (intent.hasExtra(ENTRY_ID_KEY)
&& intent.hasExtra(ENTRY_KEY) && intent.hasExtra(ENTRY_KEY)
&& intent.hasExtra(SAVE_DATABASE_KEY) && intent.hasExtra(SAVE_DATABASE_KEY)
@@ -670,20 +683,20 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
|| newEntry == null) || newEntry == null)
return null return null
mDatabase.getEntryById(entryId)?.let { oldEntry -> database.getEntryById(entryId)?.let { oldEntry ->
UpdateEntryRunnable(this, UpdateEntryRunnable(this,
mDatabase, database,
oldEntry, oldEntry,
newEntry, newEntry,
intent.getBooleanExtra(SAVE_DATABASE_KEY, false), intent.getBooleanExtra(SAVE_DATABASE_KEY, false),
AfterActionNodesRunnable()) AfterActionNodesRunnable())
} }
} else { } else {
null null
} }
} }
private fun buildDatabaseCopyNodesActionTask(intent: Intent): ActionRunnable? { private fun buildDatabaseCopyNodesActionTask(intent: Intent, database: Database): ActionRunnable? {
return if (intent.hasExtra(GROUPS_ID_KEY) return if (intent.hasExtra(GROUPS_ID_KEY)
&& intent.hasExtra(ENTRIES_ID_KEY) && intent.hasExtra(ENTRIES_ID_KEY)
&& intent.hasExtra(PARENT_ID_KEY) && intent.hasExtra(PARENT_ID_KEY)
@@ -691,20 +704,20 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
) { ) {
val parentId: NodeId<*> = intent.getParcelableExtra(PARENT_ID_KEY) ?: return null val parentId: NodeId<*> = intent.getParcelableExtra(PARENT_ID_KEY) ?: return null
mDatabase.getGroupById(parentId)?.let { newParent -> database.getGroupById(parentId)?.let { newParent ->
CopyNodesRunnable(this, CopyNodesRunnable(this,
mDatabase, database,
getListNodesFromBundle(mDatabase, intent.extras!!), getListNodesFromBundle(database, intent.extras!!),
newParent, newParent,
intent.getBooleanExtra(SAVE_DATABASE_KEY, false), intent.getBooleanExtra(SAVE_DATABASE_KEY, false),
AfterActionNodesRunnable()) AfterActionNodesRunnable())
} }
} else { } else {
null null
} }
} }
private fun buildDatabaseMoveNodesActionTask(intent: Intent): ActionRunnable? { private fun buildDatabaseMoveNodesActionTask(intent: Intent, database: Database): ActionRunnable? {
return if (intent.hasExtra(GROUPS_ID_KEY) return if (intent.hasExtra(GROUPS_ID_KEY)
&& intent.hasExtra(ENTRIES_ID_KEY) && intent.hasExtra(ENTRIES_ID_KEY)
&& intent.hasExtra(PARENT_ID_KEY) && intent.hasExtra(PARENT_ID_KEY)
@@ -712,73 +725,73 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
) { ) {
val parentId: NodeId<*> = intent.getParcelableExtra(PARENT_ID_KEY) ?: return null val parentId: NodeId<*> = intent.getParcelableExtra(PARENT_ID_KEY) ?: return null
mDatabase.getGroupById(parentId)?.let { newParent -> database.getGroupById(parentId)?.let { newParent ->
MoveNodesRunnable(this, MoveNodesRunnable(this,
mDatabase, database,
getListNodesFromBundle(mDatabase, intent.extras!!), getListNodesFromBundle(database, intent.extras!!),
newParent, newParent,
intent.getBooleanExtra(SAVE_DATABASE_KEY, false), intent.getBooleanExtra(SAVE_DATABASE_KEY, false),
AfterActionNodesRunnable()) AfterActionNodesRunnable())
} }
} else { } else {
null null
} }
} }
private fun buildDatabaseDeleteNodesActionTask(intent: Intent): ActionRunnable? { private fun buildDatabaseDeleteNodesActionTask(intent: Intent, database: Database): ActionRunnable? {
return if (intent.hasExtra(GROUPS_ID_KEY) return if (intent.hasExtra(GROUPS_ID_KEY)
&& intent.hasExtra(ENTRIES_ID_KEY) && intent.hasExtra(ENTRIES_ID_KEY)
&& intent.hasExtra(SAVE_DATABASE_KEY) && intent.hasExtra(SAVE_DATABASE_KEY)
) { ) {
DeleteNodesRunnable(this, DeleteNodesRunnable(this,
mDatabase, database,
getListNodesFromBundle(mDatabase, intent.extras!!), getListNodesFromBundle(database, intent.extras!!),
intent.getBooleanExtra(SAVE_DATABASE_KEY, false), intent.getBooleanExtra(SAVE_DATABASE_KEY, false),
AfterActionNodesRunnable()) AfterActionNodesRunnable())
} else { } else {
null null
} }
} }
private fun buildDatabaseRestoreEntryHistoryActionTask(intent: Intent): ActionRunnable? { private fun buildDatabaseRestoreEntryHistoryActionTask(intent: Intent, database: Database): ActionRunnable? {
return if (intent.hasExtra(ENTRY_ID_KEY) return if (intent.hasExtra(ENTRY_ID_KEY)
&& intent.hasExtra(ENTRY_HISTORY_POSITION_KEY) && intent.hasExtra(ENTRY_HISTORY_POSITION_KEY)
&& intent.hasExtra(SAVE_DATABASE_KEY) && intent.hasExtra(SAVE_DATABASE_KEY)
) { ) {
val entryId: NodeId<UUID> = intent.getParcelableExtra(ENTRY_ID_KEY) ?: return null val entryId: NodeId<UUID> = intent.getParcelableExtra(ENTRY_ID_KEY) ?: return null
mDatabase.getEntryById(entryId)?.let { mainEntry -> database.getEntryById(entryId)?.let { mainEntry ->
RestoreEntryHistoryDatabaseRunnable(this, RestoreEntryHistoryDatabaseRunnable(this,
mDatabase, database,
mainEntry, mainEntry,
intent.getIntExtra(ENTRY_HISTORY_POSITION_KEY, -1), intent.getIntExtra(ENTRY_HISTORY_POSITION_KEY, -1),
intent.getBooleanExtra(SAVE_DATABASE_KEY, false)) intent.getBooleanExtra(SAVE_DATABASE_KEY, false))
} }
} else { } else {
null null
} }
} }
private fun buildDatabaseDeleteEntryHistoryActionTask(intent: Intent): ActionRunnable? { private fun buildDatabaseDeleteEntryHistoryActionTask(intent: Intent, database: Database): ActionRunnable? {
return if (intent.hasExtra(ENTRY_ID_KEY) return if (intent.hasExtra(ENTRY_ID_KEY)
&& intent.hasExtra(ENTRY_HISTORY_POSITION_KEY) && intent.hasExtra(ENTRY_HISTORY_POSITION_KEY)
&& intent.hasExtra(SAVE_DATABASE_KEY) && intent.hasExtra(SAVE_DATABASE_KEY)
) { ) {
val entryId: NodeId<UUID> = intent.getParcelableExtra(ENTRY_ID_KEY) ?: return null val entryId: NodeId<UUID> = intent.getParcelableExtra(ENTRY_ID_KEY) ?: return null
mDatabase.getEntryById(entryId)?.let { mainEntry -> database.getEntryById(entryId)?.let { mainEntry ->
DeleteEntryHistoryDatabaseRunnable(this, DeleteEntryHistoryDatabaseRunnable(this,
mDatabase, database,
mainEntry, mainEntry,
intent.getIntExtra(ENTRY_HISTORY_POSITION_KEY, -1), intent.getIntExtra(ENTRY_HISTORY_POSITION_KEY, -1),
intent.getBooleanExtra(SAVE_DATABASE_KEY, false)) intent.getBooleanExtra(SAVE_DATABASE_KEY, false))
} }
} else { } else {
null null
} }
} }
private fun buildDatabaseUpdateCompressionActionTask(intent: Intent): ActionRunnable? { private fun buildDatabaseUpdateCompressionActionTask(intent: Intent, database: Database): ActionRunnable? {
return if (intent.hasExtra(OLD_ELEMENT_KEY) return if (intent.hasExtra(OLD_ELEMENT_KEY)
&& intent.hasExtra(NEW_ELEMENT_KEY) && intent.hasExtra(NEW_ELEMENT_KEY)
&& intent.hasExtra(SAVE_DATABASE_KEY)) { && intent.hasExtra(SAVE_DATABASE_KEY)) {
@@ -791,10 +804,10 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
return null return null
return UpdateCompressionBinariesDatabaseRunnable(this, return UpdateCompressionBinariesDatabaseRunnable(this,
mDatabase, database,
oldElement, oldElement,
newElement, newElement,
intent.getBooleanExtra(SAVE_DATABASE_KEY, false) intent.getBooleanExtra(SAVE_DATABASE_KEY, false)
).apply { ).apply {
mAfterSaveDatabase = { result -> mAfterSaveDatabase = { result ->
result.data = intent.extras result.data = intent.extras
@@ -805,12 +818,12 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
} }
} }
private fun buildDatabaseRemoveUnlinkedDataActionTask(intent: Intent): ActionRunnable? { private fun buildDatabaseRemoveUnlinkedDataActionTask(intent: Intent, database: Database): ActionRunnable? {
return if (intent.hasExtra(SAVE_DATABASE_KEY)) { return if (intent.hasExtra(SAVE_DATABASE_KEY)) {
return RemoveUnlinkedDataDatabaseRunnable(this, return RemoveUnlinkedDataDatabaseRunnable(this,
mDatabase, database,
intent.getBooleanExtra(SAVE_DATABASE_KEY, false) intent.getBooleanExtra(SAVE_DATABASE_KEY, false)
).apply { ).apply {
mAfterSaveDatabase = { result -> mAfterSaveDatabase = { result ->
result.data = intent.extras result.data = intent.extras
@@ -821,11 +834,11 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
} }
} }
private fun buildDatabaseUpdateElementActionTask(intent: Intent): ActionRunnable? { private fun buildDatabaseUpdateElementActionTask(intent: Intent, database: Database): ActionRunnable? {
return if (intent.hasExtra(SAVE_DATABASE_KEY)) { return if (intent.hasExtra(SAVE_DATABASE_KEY)) {
return SaveDatabaseRunnable(this, return SaveDatabaseRunnable(this,
mDatabase, database,
intent.getBooleanExtra(SAVE_DATABASE_KEY, false) intent.getBooleanExtra(SAVE_DATABASE_KEY, false)
).apply { ).apply {
mAfterSaveDatabase = { result -> mAfterSaveDatabase = { result ->
result.data = intent.extras result.data = intent.extras
@@ -839,11 +852,11 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
/** /**
* Save database without parameter * Save database without parameter
*/ */
private fun buildDatabaseSave(intent: Intent): ActionRunnable? { private fun buildDatabaseSave(intent: Intent, database: Database): ActionRunnable? {
return if (intent.hasExtra(SAVE_DATABASE_KEY)) { return if (intent.hasExtra(SAVE_DATABASE_KEY)) {
SaveDatabaseRunnable(this, SaveDatabaseRunnable(this,
mDatabase, database,
intent.getBooleanExtra(SAVE_DATABASE_KEY, false)) intent.getBooleanExtra(SAVE_DATABASE_KEY, false))
} else { } else {
null null
} }
@@ -851,8 +864,8 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
if (mDatabase.loaded) // TODO remove ?
actionOnLock() actionOnLock()
} }
companion object { companion object {

View File

@@ -125,7 +125,7 @@ fun Context.unregisterLockReceiver(lockReceiver: LockReceiver?) {
} }
} }
fun Context.closeDatabase(database: Database) { fun Context.closeDatabase(database: Database?) {
// Stop the Magikeyboard service // Stop the Magikeyboard service
stopService(Intent(this, KeyboardEntryNotificationService::class.java)) stopService(Intent(this, KeyboardEntryNotificationService::class.java))
MagikIME.removeEntry(this) MagikIME.removeEntry(this)
@@ -138,5 +138,5 @@ fun Context.closeDatabase(database: Database) {
cancelAll() cancelAll()
} }
// Clear data // Clear data
database.clearAndClose(this) database?.clearAndClose(this)
} }