diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt index 5229e7995..8c4bfed68 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt @@ -305,11 +305,11 @@ class EntryActivity : DatabaseLockActivity() { mEntryViewModel.historySelected.observe(this) { historySelected -> mDatabase?.let { database -> launch( - this, - database, - historySelected.nodeId, - historySelected.historyPosition, - mEntryActivityResultLauncher + activity = this, + database = database, + entryId = historySelected.nodeId, + historyPosition = historySelected.historyPosition, + activityResultLauncher = mEntryActivityResultLauncher ) } } @@ -471,11 +471,12 @@ class EntryActivity : DatabaseLockActivity() { R.id.menu_edit -> { mDatabase?.let { database -> mMainEntryId?.let { entryId -> - EntryEditActivity.launchToUpdate( - this, - database, - entryId, - mEntryActivityResultLauncher + EntryEditActivity.launch( + activity = this, + database = database, + registrationType = EntryEditActivity.RegistrationType.UPDATE, + nodeId = entryId, + activityResultLauncher = mEntryActivityResultLauncher ) } } @@ -527,34 +528,22 @@ class EntryActivity : DatabaseLockActivity() { const val ENTRY_FRAGMENT_TAG = "ENTRY_FRAGMENT_TAG" /** - * Open standard Entry activity + * Open standard or history Entry activity */ - fun launch(activity: Activity, - database: ContextualDatabase, - entryId: NodeId, - activityResultLauncher: ActivityResultLauncher) { + fun launch( + activity: Activity, + database: ContextualDatabase, + entryId: NodeId, + historyPosition: Int? = null, + activityResultLauncher: ActivityResultLauncher + ) { if (database.loaded) { if (TimeoutHelper.checkTimeAndLockIfTimeout(activity)) { val intent = Intent(activity, EntryActivity::class.java) intent.putExtra(KEY_ENTRY, entryId) - activityResultLauncher.launch(intent) - } - } - } - - /** - * Open history Entry activity - */ - fun launch(activity: Activity, - database: ContextualDatabase, - entryId: NodeId, - historyPosition: Int, - activityResultLauncher: ActivityResultLauncher) { - 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) + historyPosition?.let { + intent.putExtra(KEY_ENTRY_HISTORY_POSITION, historyPosition) + } activityResultLauncher.launch(intent) } } diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt index f0d8e1218..f0c2ef9ec 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt @@ -36,13 +36,10 @@ import android.widget.Spinner import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.viewModels -import androidx.annotation.RequiresApi import androidx.appcompat.app.AlertDialog -import androidx.appcompat.app.AppCompatActivity import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.core.view.isVisible import androidx.core.widget.NestedScrollView -import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity import com.google.android.material.datepicker.MaterialDatePicker import com.google.android.material.snackbar.Snackbar @@ -791,6 +788,10 @@ class EntryEditActivity : DatabaseLockActivity(), } } + enum class RegistrationType { + UPDATE, CREATE + } + companion object { private val TAG = EntryEditActivity::class.java.name @@ -800,21 +801,10 @@ class EntryEditActivity : DatabaseLockActivity(), const val KEY_PARENT = "parent" const val ADD_OR_UPDATE_ENTRY_KEY = "ADD_OR_UPDATE_ENTRY_KEY" - fun registerForEntryResult(fragment: Fragment, - entryAddedOrUpdatedListener: (NodeId?) -> Unit): ActivityResultLauncher { - return fragment.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> - if (result.resultCode == RESULT_OK) { - entryAddedOrUpdatedListener.invoke( - result.data?.getParcelableExtraCompat(ADD_OR_UPDATE_ENTRY_KEY) - ) - } else { - entryAddedOrUpdatedListener.invoke(null) - } - } - } - - fun registerForEntryResult(activity: FragmentActivity, - entryAddedOrUpdatedListener: (NodeId?) -> Unit): ActivityResultLauncher { + fun registerForEntryResult( + activity: FragmentActivity, + entryAddedOrUpdatedListener: (NodeId?) -> Unit + ): ActivityResultLauncher { return activity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == RESULT_OK) { entryAddedOrUpdatedListener.invoke( @@ -827,142 +817,74 @@ class EntryEditActivity : DatabaseLockActivity(), } /** - * Launch EntryEditActivity to update an existing entry by his [entryId] + * Launch EntryEditActivity to update an existing entry or to add a new entry in an existing group */ - fun launchToUpdate(activity: Activity, - database: ContextualDatabase, - entryId: NodeId, - activityResultLauncher: ActivityResultLauncher) { + fun launch( + activity: Activity, + database: ContextualDatabase, + registrationType: RegistrationType, + nodeId: NodeId<*>, + activityResultLauncher: ActivityResultLauncher + ) { if (database.loaded && !database.isReadOnly) { if (TimeoutHelper.checkTimeAndLockIfTimeout(activity)) { val intent = Intent(activity, EntryEditActivity::class.java) - intent.putExtra(KEY_ENTRY, entryId) + when (registrationType) { + RegistrationType.UPDATE -> intent.putExtra(KEY_ENTRY, nodeId) + RegistrationType.CREATE -> intent.putExtra(KEY_PARENT, nodeId) + } activityResultLauncher.launch(intent) } } } /** - * Launch EntryEditActivity to add a new entry in an existent group + * Launch EntryEditActivity to add a new entry in special selection */ - fun launchToCreate(activity: Activity, - database: ContextualDatabase, - groupId: NodeId<*>, - activityResultLauncher: ActivityResultLauncher) { - if (database.loaded && !database.isReadOnly) { - if (TimeoutHelper.checkTimeAndLockIfTimeout(activity)) { - val intent = Intent(activity, EntryEditActivity::class.java) - intent.putExtra(KEY_PARENT, groupId) - activityResultLauncher.launch(intent) - } - } - } - - /** - * Launch EntryEditActivity to add a new entry in keyboard selection - */ - fun launchForKeyboardSelectionResult(context: Context, - database: ContextualDatabase, - groupId: NodeId<*>, - searchInfo: SearchInfo? = null) { + fun launchForSelection( + context: Context, + database: ContextualDatabase, + typeMode: TypeMode, + groupId: NodeId<*>, + searchInfo: SearchInfo? = null, + autofillComponent: AutofillComponent? = null, + activityResultLauncher: ActivityResultLauncher? = null, + ) { if (database.loaded && !database.isReadOnly) { if (TimeoutHelper.checkTimeAndLockIfTimeout(context)) { val intent = Intent(context, EntryEditActivity::class.java) intent.putExtra(KEY_PARENT, groupId) - EntrySelectionHelper.startActivityForKeyboardSelectionModeResult( - context, - intent, - searchInfo + EntrySelectionHelper.startActivityForSelectionModeResult( + context = context, + intent = intent, + typeMode = typeMode, + searchInfo = searchInfo, + autofillComponent = autofillComponent, + activityResultLauncher = activityResultLauncher ) } } } /** - * Launch EntryEditActivity to add a new entry in autofill selection + * Launch EntryEditActivity to update an updated entry or register a new entry (from autofill) */ - @RequiresApi(api = Build.VERSION_CODES.O) - fun launchForAutofillResult(activity: AppCompatActivity, - database: ContextualDatabase, - activityResultLauncher: ActivityResultLauncher?, - autofillComponent: AutofillComponent, - groupId: NodeId<*>, - searchInfo: SearchInfo? = null) { - if (database.loaded && !database.isReadOnly) { - if (TimeoutHelper.checkTimeAndLockIfTimeout(activity)) { - val intent = Intent(activity, EntryEditActivity::class.java) - intent.putExtra(KEY_PARENT, groupId) - EntrySelectionHelper.startActivityForAutofillSelectionModeResult( - activity, - intent, - activityResultLauncher, - autofillComponent, - searchInfo - ) - } - } - } - - /** - * Launch EntryEditActivity to add a new passkey entry - */ - @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) - fun launchForPasskeySelectionResult(context: Context, - database: ContextualDatabase, - activityResultLauncher: ActivityResultLauncher?, - groupId: NodeId<*>, - searchInfo: SearchInfo? = null) { + fun launchForRegistration( + context: Context, + database: ContextualDatabase, + nodeId: NodeId<*>, + registerInfo: RegisterInfo? = null, + typeMode: TypeMode, + registrationType: RegistrationType, + activityResultLauncher: ActivityResultLauncher? = null, + ) { if (database.loaded && !database.isReadOnly) { if (TimeoutHelper.checkTimeAndLockIfTimeout(context)) { val intent = Intent(context, EntryEditActivity::class.java) - intent.putExtra(KEY_PARENT, groupId) - EntrySelectionHelper.startActivityForPasskeySelectionModeResult( - context, - intent, - activityResultLauncher, - searchInfo - ) - } - } - } - - /** - * Launch EntryEditActivity to register an updated entry (from autofill) - */ - fun launchToUpdateForRegistration(context: Context, - database: ContextualDatabase, - activityResultLauncher: ActivityResultLauncher?, - entryId: NodeId, - registerInfo: RegisterInfo?, - typeMode: TypeMode) { - if (database.loaded && !database.isReadOnly) { - if (TimeoutHelper.checkTimeAndLockIfTimeout(context)) { - val intent = Intent(context, EntryEditActivity::class.java) - intent.putExtra(KEY_ENTRY, entryId) - EntrySelectionHelper.startActivityForRegistrationModeResult( - context, - activityResultLauncher, - intent, - registerInfo, - typeMode - ) - } - } - } - - /** - * Launch EntryEditActivity to register a new entry (from autofill) - */ - fun launchToCreateForRegistration(context: Context, - database: ContextualDatabase, - activityResultLauncher: ActivityResultLauncher?, - groupId: NodeId<*>, - registerInfo: RegisterInfo? = null, - typeMode: TypeMode) { - if (database.loaded && !database.isReadOnly) { - if (TimeoutHelper.checkTimeAndLockIfTimeout(context)) { - val intent = Intent(context, EntryEditActivity::class.java) - intent.putExtra(KEY_PARENT, groupId) + when (registrationType) { + RegistrationType.UPDATE -> intent.putExtra(KEY_ENTRY, nodeId) + RegistrationType.CREATE -> intent.putExtra(KEY_PARENT, nodeId) + } EntrySelectionHelper.startActivityForRegistrationModeResult( context, activityResultLauncher, diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/FileDatabaseSelectActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/FileDatabaseSelectActivity.kt index f3e129984..a304fbf47 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/FileDatabaseSelectActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/FileDatabaseSelectActivity.kt @@ -33,8 +33,6 @@ import android.view.MenuItem import android.view.View import androidx.activity.result.ActivityResultLauncher import androidx.activity.viewModels -import androidx.annotation.RequiresApi -import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.core.view.isVisible @@ -50,7 +48,6 @@ import com.kunzisoft.keepass.activities.legacy.DatabaseModeActivity import com.kunzisoft.keepass.adapters.FileDatabaseHistoryAdapter import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction import com.kunzisoft.keepass.credentialprovider.EntrySelectionHelper -import com.kunzisoft.keepass.credentialprovider.EntrySelectionHelper.buildActivityResultLauncher import com.kunzisoft.keepass.credentialprovider.SpecialMode import com.kunzisoft.keepass.credentialprovider.TypeMode import com.kunzisoft.keepass.credentialprovider.autofill.AutofillComponent @@ -128,7 +125,7 @@ class FileDatabaseSelectActivity : DatabaseModeActivity(), mExternalFileHelper = ExternalFileHelper(this) mExternalFileHelper?.buildOpenDocument { uri -> uri?.let { - launchPasswordActivityWithPath(uri) + launchMainCredentialActivityWithPath(uri) } } mExternalFileHelper?.buildCreateDocument("application/x-keepass") { databaseFileCreatedUri -> @@ -157,7 +154,7 @@ class FileDatabaseSelectActivity : DatabaseModeActivity(), } mAdapterDatabaseHistory?.setOnFileDatabaseHistoryOpenListener { fileDatabaseHistoryEntityToOpen -> fileDatabaseHistoryEntityToOpen.databaseUri?.let { databaseFileUri -> - launchPasswordActivity( + launchMainCredentialActivity( databaseFileUri, fileDatabaseHistoryEntityToOpen.keyFileUri, fileDatabaseHistoryEntityToOpen.hardwareKey @@ -176,7 +173,7 @@ class FileDatabaseSelectActivity : DatabaseModeActivity(), // Load default database the first time databaseFilesViewModel.doForDefaultDatabase { databaseFileUri -> - launchPasswordActivityWithPath(databaseFileUri) + launchMainCredentialActivityWithPath(databaseFileUri) } // Retrieve the database URI provided by file manager after an orientation change @@ -284,17 +281,104 @@ class FileDatabaseSelectActivity : DatabaseModeActivity(), Snackbar.make(coordinatorLayout, error, Snackbar.LENGTH_LONG).asError().show() } - private fun launchPasswordActivity(databaseUri: Uri, keyFile: Uri?, hardwareKey: HardwareKey?) { - MainCredentialActivity.launch(this, - databaseUri, - keyFile, - hardwareKey, - { exception -> - fileNoFoundAction(exception) + private fun launchMainCredentialActivity(databaseUri: Uri, keyFile: Uri?, hardwareKey: HardwareKey?) { + try { + EntrySelectionHelper.doSpecialAction( + intent = this.intent, + defaultAction = { + MainCredentialActivity.launch( + activity = this, + databaseFile = databaseUri, + keyFile = keyFile, + hardwareKey = hardwareKey + ) }, - { onCancelSpecialMode() }, - { onLaunchActivitySpecialMode() }, - mCredentialActivityResultLauncher) + searchAction = { searchInfo -> + MainCredentialActivity.launchForSearchResult( + activity = this, + databaseFile = databaseUri, + keyFile = keyFile, + hardwareKey = hardwareKey, + searchInfo = searchInfo + ) + onLaunchActivitySpecialMode() + }, + registrationAction = { registerInfo -> + MainCredentialActivity.launchForRegistration( + activity = this, + activityResultLauncher = mCredentialActivityResultLauncher, + databaseFile = databaseUri, + keyFile = keyFile, + hardwareKey = hardwareKey, + typeMode = TypeMode.DEFAULT, + registerInfo = registerInfo + ) + onLaunchActivitySpecialMode() + }, + keyboardSelectionAction = { searchInfo -> + MainCredentialActivity.launchForSelection( + activity = this, + databaseFile = databaseUri, + keyFile = keyFile, + hardwareKey = hardwareKey, + typeMode = TypeMode.MAGIKEYBOARD, + searchInfo = searchInfo + ) + onLaunchActivitySpecialMode() + }, + autofillSelectionAction = { searchInfo, autofillComponent -> + MainCredentialActivity.launchForSelection( + activity = this, + activityResultLauncher = mCredentialActivityResultLauncher, + databaseFile = databaseUri, + keyFile = keyFile, + hardwareKey = hardwareKey, + typeMode = TypeMode.AUTOFILL, + searchInfo = searchInfo, + autofillComponent = autofillComponent, + ) + onLaunchActivitySpecialMode() + }, + autofillRegistrationAction = { registerInfo -> + MainCredentialActivity.launchForRegistration( + activity = this, + activityResultLauncher = mCredentialActivityResultLauncher, + databaseFile = databaseUri, + keyFile = keyFile, + hardwareKey = hardwareKey, + typeMode = TypeMode.AUTOFILL, + registerInfo = registerInfo + ) + onLaunchActivitySpecialMode() + }, + passkeySelectionAction = { searchInfo -> + MainCredentialActivity.launchForSelection( + activity = this, + activityResultLauncher = mCredentialActivityResultLauncher, + databaseFile = databaseUri, + keyFile = keyFile, + hardwareKey = hardwareKey, + typeMode = TypeMode.PASSKEY, + searchInfo = searchInfo + ) + onLaunchActivitySpecialMode() + }, + passkeyRegistrationAction = { registerInfo -> + MainCredentialActivity.launchForRegistration( + activity = this, + activityResultLauncher = mCredentialActivityResultLauncher, + databaseFile = databaseUri, + keyFile = keyFile, + hardwareKey = hardwareKey, + typeMode = TypeMode.PASSKEY, + registerInfo = registerInfo + ) + onLaunchActivitySpecialMode() + } + ) + } catch (e: FileNotFoundException) { + fileNoFoundAction(e) + } } private fun launchGroupActivityIfLoaded(database: ContextualDatabase) { @@ -308,8 +392,8 @@ class FileDatabaseSelectActivity : DatabaseModeActivity(), } } - private fun launchPasswordActivityWithPath(databaseUri: Uri) { - launchPasswordActivity(databaseUri, null, null) + private fun launchMainCredentialActivityWithPath(databaseUri: Uri) { + launchMainCredentialActivity(databaseUri, null, null) // Delete flickering for kitkat <= @Suppress("DEPRECATION") if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) @@ -439,58 +523,37 @@ class FileDatabaseSelectActivity : DatabaseModeActivity(), * ------------------------- */ - fun launchForSearchResult(context: Context, - searchInfo: SearchInfo) { - EntrySelectionHelper.startActivityForSearchModeResult(context, - Intent(context, FileDatabaseSelectActivity::class.java), - searchInfo) + fun launchForSearchResult( + context: Context, + searchInfo: SearchInfo + ) { + EntrySelectionHelper.startActivityForSearchModeResult( + context = context, + intent = Intent(context, FileDatabaseSelectActivity::class.java), + searchInfo = searchInfo + ) } /* * ------------------------- - * Keyboard Launch + * Selection Launch * ------------------------- */ - fun launchForKeyboardSelectionResult(activity: Activity, - searchInfo: SearchInfo? = null) { - EntrySelectionHelper.startActivityForKeyboardSelectionModeResult(activity, - Intent(activity, FileDatabaseSelectActivity::class.java), - searchInfo) - } - - /* - * ------------------------- - * Autofill Launch - * ------------------------- - */ - - @RequiresApi(api = Build.VERSION_CODES.O) - fun launchForAutofillResult(activity: AppCompatActivity, - activityResultLauncher: ActivityResultLauncher?, - autofillComponent: AutofillComponent, - searchInfo: SearchInfo? = null) { - EntrySelectionHelper.startActivityForAutofillSelectionModeResult(activity, - Intent(activity, FileDatabaseSelectActivity::class.java), - activityResultLauncher, - autofillComponent, - searchInfo) - } - - /* - * ------------------------- - * Passkey Launch - * ------------------------- - */ - @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) - fun launchForPasskeySelectionResult(activity: Activity, - activityResultLauncher: ActivityResultLauncher?, - searchInfo: SearchInfo? = null) { - EntrySelectionHelper.startActivityForPasskeySelectionModeResult( - activity, - Intent(activity, FileDatabaseSelectActivity::class.java), - activityResultLauncher, - searchInfo + fun launchForSelection( + activity: Activity, + typeMode: TypeMode, + searchInfo: SearchInfo? = null, + autofillComponent: AutofillComponent? = null, + activityResultLauncher: ActivityResultLauncher? = null, + ) { + EntrySelectionHelper.startActivityForSelectionModeResult( + context = activity, + intent = Intent(activity, FileDatabaseSelectActivity::class.java), + searchInfo = searchInfo, + typeMode = typeMode, + autofillComponent = autofillComponent, + activityResultLauncher = activityResultLauncher ) } @@ -499,16 +562,18 @@ class FileDatabaseSelectActivity : DatabaseModeActivity(), * Registration Launch * ------------------------- */ - fun launchForRegistration(context: Context, - activityResultLauncher: ActivityResultLauncher?, - registerInfo: RegisterInfo? = null, - typeMode: TypeMode) { + fun launchForRegistration( + context: Context, + activityResultLauncher: ActivityResultLauncher?, + registerInfo: RegisterInfo? = null, + typeMode: TypeMode + ) { EntrySelectionHelper.startActivityForRegistrationModeResult( - context, - activityResultLauncher, - Intent(context, FileDatabaseSelectActivity::class.java), - registerInfo, - typeMode + context = context, + activityResultLauncher = activityResultLauncher, + intent = Intent(context, FileDatabaseSelectActivity::class.java), + registerInfo = registerInfo, + typeMode = typeMode ) } } diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt index 057054e1b..9c39a0f99 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt @@ -41,7 +41,6 @@ import android.widget.ProgressBar import android.widget.TextView import androidx.activity.result.ActivityResultLauncher import androidx.activity.viewModels -import androidx.annotation.RequiresApi import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.view.ActionMode @@ -66,7 +65,6 @@ import com.kunzisoft.keepass.activities.helpers.ExternalFileHelper import com.kunzisoft.keepass.activities.legacy.DatabaseLockActivity import com.kunzisoft.keepass.adapters.BreadcrumbAdapter import com.kunzisoft.keepass.credentialprovider.EntrySelectionHelper -import com.kunzisoft.keepass.credentialprovider.EntrySelectionHelper.buildActivityResultLauncher import com.kunzisoft.keepass.credentialprovider.SpecialMode import com.kunzisoft.keepass.credentialprovider.TypeMode import com.kunzisoft.keepass.credentialprovider.autofill.AutofillComponent @@ -486,10 +484,11 @@ class GroupActivity : DatabaseLockActivity(), intent = intent, defaultAction = { mMainGroup?.nodeId?.let { currentParentGroupId -> - EntryEditActivity.launchToCreate( + EntryEditActivity.launch( activity = this@GroupActivity, database = database, - groupId = currentParentGroupId, + registrationType = EntryEditActivity.RegistrationType.CREATE, + nodeId = currentParentGroupId, activityResultLauncher = mEntryActivityResultLauncher ) } @@ -498,73 +497,69 @@ class GroupActivity : DatabaseLockActivity(), // Search not used }, registrationAction = { registerInfo -> - EntryEditActivity.launchToCreateForRegistration( + EntryEditActivity.launchForRegistration( context = this@GroupActivity, database = database, - activityResultLauncher = null, - groupId = currentGroup.nodeId, + nodeId = currentGroup.nodeId, registerInfo = registerInfo, - typeMode = TypeMode.DEFAULT + typeMode = TypeMode.DEFAULT, + registrationType = EntryEditActivity.RegistrationType.CREATE ) onLaunchActivitySpecialMode() }, keyboardSelectionAction = { searchInfo -> - EntryEditActivity.launchForKeyboardSelectionResult( + EntryEditActivity.launchForSelection( context = this@GroupActivity, database = database, + typeMode = TypeMode.MAGIKEYBOARD, groupId = currentGroup.nodeId, searchInfo = searchInfo ) onLaunchActivitySpecialMode() }, autofillSelectionAction = { searchInfo, autofillComponent -> - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - EntryEditActivity.launchForAutofillResult( - activity = this@GroupActivity, - database = database, - activityResultLauncher = mCredentialActivityResultLauncher, - autofillComponent = autofillComponent, - groupId = currentGroup.nodeId, - searchInfo = searchInfo - ) - onLaunchActivitySpecialMode() - } else { - onCancelSpecialMode() - } - }, - autofillRegistrationAction = { registerInfo -> - EntryEditActivity.launchToCreateForRegistration( + EntryEditActivity.launchForSelection( context = this@GroupActivity, database = database, - activityResultLauncher = null, + typeMode = TypeMode.AUTOFILL, groupId = currentGroup.nodeId, + searchInfo = searchInfo, + autofillComponent = autofillComponent, + activityResultLauncher = mCredentialActivityResultLauncher + ) + onLaunchActivitySpecialMode() + }, + autofillRegistrationAction = { registerInfo -> + EntryEditActivity.launchForRegistration( + context = this@GroupActivity, + database = database, + nodeId = currentGroup.nodeId, registerInfo = registerInfo, - typeMode = TypeMode.AUTOFILL + typeMode = TypeMode.AUTOFILL, + registrationType = EntryEditActivity.RegistrationType.CREATE ) onLaunchActivitySpecialMode() }, passkeySelectionAction = { searchInfo -> - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { - EntryEditActivity.launchForPasskeySelectionResult( - context = this@GroupActivity, - database = database, - activityResultLauncher = mCredentialActivityResultLauncher, - groupId = currentGroup.nodeId, - searchInfo = searchInfo, - ) - onLaunchActivitySpecialMode() - } else { - onCancelSpecialMode() - } + EntryEditActivity.launchForSelection( + context = this@GroupActivity, + database = database, + typeMode = TypeMode.PASSKEY, + groupId = currentGroup.nodeId, + searchInfo = searchInfo, + activityResultLauncher = mCredentialActivityResultLauncher + ) + onLaunchActivitySpecialMode() }, passkeyRegistrationAction = { registerInfo -> - EntryEditActivity.launchToCreateForRegistration( + EntryEditActivity.launchForRegistration( context = this@GroupActivity, database = database, activityResultLauncher = mCredentialActivityResultLauncher, - groupId = currentGroup.nodeId, + nodeId = currentGroup.nodeId, registerInfo = registerInfo, - typeMode = TypeMode.PASSKEY + typeMode = TypeMode.PASSKEY, + registrationType = EntryEditActivity.RegistrationType.CREATE ) onLaunchActivitySpecialMode() } @@ -1018,13 +1013,14 @@ class GroupActivity : DatabaseLockActivity(), ) { removeSearch() // Registration to update the entry - EntryEditActivity.launchToUpdateForRegistration( + EntryEditActivity.launchForRegistration( context = this@GroupActivity, database = database, activityResultLauncher = activityResultLauncher, - entryId = entry.nodeId, + nodeId = entry.nodeId, registerInfo = registerInfo, - typeMode = typeMode + typeMode = typeMode, + registrationType = EntryEditActivity.RegistrationType.UPDATE ) onLaunchActivitySpecialMode() } @@ -1094,11 +1090,12 @@ class GroupActivity : DatabaseLockActivity(), launchDialogForGroupUpdate(node as Group) } Type.ENTRY -> { - EntryEditActivity.launchToUpdate( - this@GroupActivity, - database, - (node as Entry).nodeId, - mEntryActivityResultLauncher + EntryEditActivity.launch( + activity = this@GroupActivity, + database = database, + registrationType = EntryEditActivity.RegistrationType.UPDATE, + nodeId = (node as Entry).nodeId, + activityResultLauncher = mEntryActivityResultLauncher ) } } @@ -1536,9 +1533,11 @@ class GroupActivity : DatabaseLockActivity(), private const val OLD_GROUP_TO_UPDATE_KEY = "OLD_GROUP_TO_UPDATE_KEY" private const val AUTO_SEARCH_KEY = "AUTO_SEARCH_KEY" - private fun buildIntent(context: Context, - groupState: GroupState?, - intentBuildLauncher: (Intent) -> Unit) { + private fun buildIntent( + context: Context, + groupState: GroupState?, + intentBuildLauncher: (Intent) -> Unit + ) { val intent = Intent(context, GroupActivity::class.java) if (groupState != null) { intent.putExtra(GROUP_STATE_KEY, groupState) @@ -1546,18 +1545,12 @@ class GroupActivity : DatabaseLockActivity(), intentBuildLauncher.invoke(intent) } - private fun checkTimeAndBuildIntent(activity: Activity, - groupState: GroupState?, - intentBuildLauncher: (Intent) -> Unit) { - if (TimeoutHelper.checkTimeAndLockIfTimeout(activity)) { - buildIntent(activity, groupState, intentBuildLauncher) - } - } - - private fun checkTimeAndBuildIntent(context: Context, - groupState: GroupState?, - intentBuildLauncher: (Intent) -> Unit) { - if (TimeoutHelper.checkTime(context)) { + private fun checkTimeAndBuildIntent( + context: Context, + groupState: GroupState?, + intentBuildLauncher: (Intent) -> Unit + ) { + if (TimeoutHelper.checkTimeAndLockIfTimeout(context)) { buildIntent(context, groupState, intentBuildLauncher) } } @@ -1567,9 +1560,11 @@ class GroupActivity : DatabaseLockActivity(), * Standard Launch * ------------------------- */ - fun launch(context: Context, - database: ContextualDatabase, - autoSearch: Boolean = false) { + fun launch( + context: Context, + database: ContextualDatabase, + autoSearch: Boolean = false + ) { if (database.loaded) { checkTimeAndBuildIntent(context, null) { intent -> intent.putExtra(AUTO_SEARCH_KEY, autoSearch) @@ -1583,10 +1578,12 @@ class GroupActivity : DatabaseLockActivity(), * Search Launch * ------------------------- */ - fun launchForSearchResult(context: Context, - database: ContextualDatabase, - searchInfo: SearchInfo, - autoSearch: Boolean = false) { + fun launchForSearchResult( + context: Context, + database: ContextualDatabase, + searchInfo: SearchInfo, + autoSearch: Boolean = false + ) { if (database.loaded) { checkTimeAndBuildIntent(context, null) { intent -> intent.putExtra(AUTO_SEARCH_KEY, autoSearch) @@ -1599,72 +1596,25 @@ class GroupActivity : DatabaseLockActivity(), } } - /* - * ------------------------- - * Keyboard Launch - * ------------------------- - */ - fun launchForKeyboardSelectionResult(context: Context, - database: ContextualDatabase, - searchInfo: SearchInfo? = null, - autoSearch: Boolean = false) { + fun launchForSelection( + context: Context, + database: ContextualDatabase, + typeMode: TypeMode, + searchInfo: SearchInfo? = null, + autoSearch: Boolean = false, + autofillComponent: AutofillComponent? = null, + activityResultLauncher: ActivityResultLauncher? = null, + ) { if (database.loaded) { checkTimeAndBuildIntent(context, null) { intent -> intent.putExtra(AUTO_SEARCH_KEY, autoSearch) - EntrySelectionHelper.startActivityForKeyboardSelectionModeResult( - context, - intent, - searchInfo - ) - } - } - } - - /* - * ------------------------- - * Autofill Launch - * ------------------------- - */ - @RequiresApi(api = Build.VERSION_CODES.O) - fun launchForAutofillSelectionResult(activity: AppCompatActivity, - database: ContextualDatabase, - activityResultLauncher: ActivityResultLauncher?, - autofillComponent: AutofillComponent, - searchInfo: SearchInfo? = null, - autoSearch: Boolean = false) { - if (database.loaded) { - checkTimeAndBuildIntent(activity, null) { intent -> - intent.putExtra(AUTO_SEARCH_KEY, autoSearch) - EntrySelectionHelper.startActivityForAutofillSelectionModeResult( - activity, - intent, - activityResultLauncher, - autofillComponent, - searchInfo - ) - } - } - } - - /* - * ------------------------- - * Passkey Launch - * ------------------------- - */ - @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) - fun launchForPasskeySelectionResult(context: Context, - database: ContextualDatabase, - activityResultLauncher: ActivityResultLauncher?, - searchInfo: SearchInfo? = null, - autoSearch: Boolean = false) { - if (database.loaded) { - checkTimeAndBuildIntent(context, null) { intent -> - intent.putExtra(AUTO_SEARCH_KEY, autoSearch) - EntrySelectionHelper.startActivityForPasskeySelectionModeResult( - context, - intent, - activityResultLauncher, - searchInfo + EntrySelectionHelper.startActivityForSelectionModeResult( + context = context, + intent = intent, + typeMode = typeMode, + searchInfo = searchInfo, + autofillComponent = autofillComponent, + activityResultLauncher = activityResultLauncher ) } } @@ -1675,11 +1625,13 @@ class GroupActivity : DatabaseLockActivity(), * Registration Launch * ------------------------- */ - fun launchForRegistration(context: Context, - activityResultLauncher: ActivityResultLauncher?, - database: ContextualDatabase, - registerInfo: RegisterInfo? = null, - typeMode: TypeMode) { + fun launchForRegistration( + context: Context, + activityResultLauncher: ActivityResultLauncher?, + database: ContextualDatabase, + registerInfo: RegisterInfo? = null, + typeMode: TypeMode + ) { if (database.loaded && !database.isReadOnly) { checkTimeAndBuildIntent(context, null) { intent -> intent.putExtra(AUTO_SEARCH_KEY, false) @@ -1699,12 +1651,14 @@ class GroupActivity : DatabaseLockActivity(), * Global Launch * ------------------------- */ - fun launch(activity: AppCompatActivity, - database: ContextualDatabase, - onValidateSpecialMode: () -> Unit, - onCancelSpecialMode: () -> Unit, - onLaunchActivitySpecialMode: () -> Unit, - activityResultLauncher: ActivityResultLauncher?) { + fun launch( + activity: AppCompatActivity, + database: ContextualDatabase, + onValidateSpecialMode: () -> Unit, + onCancelSpecialMode: () -> Unit, + onLaunchActivitySpecialMode: () -> Unit, + activityResultLauncher: ActivityResultLauncher? + ) { EntrySelectionHelper.doSpecialAction( intent = activity.intent, defaultAction = { @@ -1764,20 +1718,26 @@ class GroupActivity : DatabaseLockActivity(), onValidateSpecialMode() }, { autoSearch -> - launchForKeyboardSelectionResult(activity, - database, - searchInfo, - autoSearch) + launchForSelection( + context = activity, + database = database, + typeMode = TypeMode.MAGIKEYBOARD, + searchInfo = searchInfo, + autoSearch = autoSearch + ) onLaunchActivitySpecialMode() } ) }, onItemNotFound = { // Here no search info found, disable auto search - launchForKeyboardSelectionResult(activity, - database, - searchInfo, - false) + launchForSelection( + context = activity, + database = database, + typeMode = TypeMode.MAGIKEYBOARD, + searchInfo = searchInfo, + autoSearch = false + ) onLaunchActivitySpecialMode() }, onDatabaseClosed = { @@ -1788,35 +1748,33 @@ class GroupActivity : DatabaseLockActivity(), }, autofillSelectionAction = { searchInfo, autofillComponent -> // Autofill selection - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - SearchHelper.checkAutoSearchInfo( - context = activity, - database = database, - searchInfo = searchInfo, - onItemsFound = { openedDatabase, items -> - // Response is build - AutofillHelper.buildResponseAndSetResult(activity, openedDatabase, items) - onValidateSpecialMode() - }, - onItemNotFound = { - // Here no search info found, disable auto search - launchForAutofillSelectionResult( - activity = activity, - database = database, - autofillComponent = autofillComponent, - searchInfo = searchInfo, - autoSearch = false, - activityResultLauncher = activityResultLauncher) - onLaunchActivitySpecialMode() - }, - onDatabaseClosed = { - // Simply close if database not opened, normally not happened - onCancelSpecialMode() - } - ) - } else { - onCancelSpecialMode() - } + SearchHelper.checkAutoSearchInfo( + context = activity, + database = database, + searchInfo = searchInfo, + onItemsFound = { openedDatabase, items -> + // Response is build + AutofillHelper.buildResponseAndSetResult(activity, openedDatabase, items) + onValidateSpecialMode() + }, + onItemNotFound = { + // Here no search info found, disable auto search + launchForSelection( + context = activity, + database = database, + typeMode = TypeMode.AUTOFILL, + searchInfo = searchInfo, + autoSearch = false, + autofillComponent = autofillComponent, + activityResultLauncher = activityResultLauncher + ) + onLaunchActivitySpecialMode() + }, + onDatabaseClosed = { + // Simply close if database not opened, normally not happened + onCancelSpecialMode() + } + ) }, autofillRegistrationAction = { registerInfo -> // Autofill registration @@ -1859,49 +1817,47 @@ class GroupActivity : DatabaseLockActivity(), }, passkeySelectionAction = { searchInfo -> // Passkey selection - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { - SearchHelper.checkAutoSearchInfo( - context = activity, - database = database, - searchInfo = searchInfo, - onItemsFound = { _, items -> - // Response is build - EntrySelectionHelper.performSelection( - items = items, - actionPopulateCredentialProvider = { entryInfo -> - activity.buildPasskeyResponseAndSetResult(entryInfo) - onValidateSpecialMode() - }, - actionEntrySelection = { - launchForPasskeySelectionResult( - context = activity, - database = database, - searchInfo = searchInfo, - activityResultLauncher = activityResultLauncher, - autoSearch = true - ) - onLaunchActivitySpecialMode() - } - ) - }, - onItemNotFound = { - // Here no search info found, disable auto search - launchForPasskeySelectionResult( - context = activity, - database = database, - searchInfo = searchInfo, - activityResultLauncher = activityResultLauncher - ) - onLaunchActivitySpecialMode() - }, - onDatabaseClosed = { - // Simply close if database not opened, normally not happened - onCancelSpecialMode() - } - ) - } else { - onCancelSpecialMode() - } + SearchHelper.checkAutoSearchInfo( + context = activity, + database = database, + searchInfo = searchInfo, + onItemsFound = { _, items -> + // Response is build + EntrySelectionHelper.performSelection( + items = items, + actionPopulateCredentialProvider = { entryInfo -> + activity.buildPasskeyResponseAndSetResult(entryInfo) + onValidateSpecialMode() + }, + actionEntrySelection = { + launchForSelection( + context = activity, + database = database, + typeMode = TypeMode.PASSKEY, + searchInfo = searchInfo, + activityResultLauncher = activityResultLauncher, + autoSearch = true + ) + onLaunchActivitySpecialMode() + } + ) + }, + onItemNotFound = { + // Here no search info found, disable auto search + launchForSelection( + context = activity, + database = database, + typeMode = TypeMode.PASSKEY, + searchInfo = searchInfo, + activityResultLauncher = activityResultLauncher + ) + onLaunchActivitySpecialMode() + }, + onDatabaseClosed = { + // Simply close if database not opened, normally not happened + onCancelSpecialMode() + } + ) }, passkeyRegistrationAction = { registerInfo -> // Passkey registration diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/MainCredentialActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/MainCredentialActivity.kt index 7dc0255a2..4ff2dae68 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/MainCredentialActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/MainCredentialActivity.kt @@ -36,7 +36,6 @@ import android.widget.TextView import android.widget.Toast import androidx.activity.result.ActivityResultLauncher import androidx.activity.viewModels -import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar import androidx.biometric.BiometricManager @@ -56,7 +55,6 @@ import com.kunzisoft.keepass.biometric.DeviceUnlockFragment import com.kunzisoft.keepass.biometric.DeviceUnlockManager import com.kunzisoft.keepass.biometric.deviceUnlockError import com.kunzisoft.keepass.credentialprovider.EntrySelectionHelper -import com.kunzisoft.keepass.credentialprovider.EntrySelectionHelper.buildActivityResultLauncher import com.kunzisoft.keepass.credentialprovider.SpecialMode import com.kunzisoft.keepass.credentialprovider.TypeMode import com.kunzisoft.keepass.credentialprovider.autofill.AutofillComponent @@ -746,11 +744,13 @@ class MainCredentialActivity : DatabaseModeActivity() { private const val KEY_PASSWORD = "password" private const val KEY_LAUNCH_IMMEDIATELY = "launchImmediately" - private fun buildAndLaunchIntent(activity: Activity, - databaseFile: Uri, - keyFile: Uri?, - hardwareKey: HardwareKey?, - intentBuildLauncher: (Intent) -> Unit) { + private fun buildAndLaunchIntent( + activity: Activity, + databaseFile: Uri, + keyFile: Uri?, + hardwareKey: HardwareKey?, + intentBuildLauncher: (Intent) -> Unit + ) { val intent = Intent(activity, MainCredentialActivity::class.java) intent.putExtra(KEY_FILENAME, databaseFile) if (keyFile != null) @@ -767,10 +767,12 @@ class MainCredentialActivity : DatabaseModeActivity() { */ @Throws(FileNotFoundException::class) - fun launch(activity: Activity, - databaseFile: Uri, - keyFile: Uri?, - hardwareKey: HardwareKey?) { + fun launch( + activity: Activity, + databaseFile: Uri, + keyFile: Uri?, + hardwareKey: HardwareKey? + ) { buildAndLaunchIntent(activity, databaseFile, keyFile, hardwareKey) { intent -> activity.startActivity(intent) } @@ -783,92 +785,58 @@ class MainCredentialActivity : DatabaseModeActivity() { */ @Throws(FileNotFoundException::class) - fun launchForSearchResult(activity: Activity, - databaseFile: Uri, - keyFile: Uri?, - hardwareKey: HardwareKey?, - searchInfo: SearchInfo) { + fun launchForSearchResult( + activity: Activity, + databaseFile: Uri, + keyFile: Uri?, + hardwareKey: HardwareKey?, + searchInfo: SearchInfo + ) { buildAndLaunchIntent(activity, databaseFile, keyFile, hardwareKey) { intent -> EntrySelectionHelper.startActivityForSearchModeResult( - activity, - intent, - searchInfo) - } - } - - /* - * ------------------------- - * Keyboard Launch - * ------------------------- - */ - - @Throws(FileNotFoundException::class) - fun launchForKeyboardResult(activity: Activity, - databaseFile: Uri, - keyFile: Uri?, - hardwareKey: HardwareKey?, - searchInfo: SearchInfo?) { - buildAndLaunchIntent(activity, databaseFile, keyFile, hardwareKey) { intent -> - EntrySelectionHelper.startActivityForKeyboardSelectionModeResult( - activity, - intent, - searchInfo) - } - } - - /* - * ------------------------- - * Autofill Launch - * ------------------------- - */ - - @RequiresApi(api = Build.VERSION_CODES.O) - @Throws(FileNotFoundException::class) - fun launchForAutofillResult(activity: AppCompatActivity, - activityResultLauncher: ActivityResultLauncher?, - databaseFile: Uri, - keyFile: Uri?, - hardwareKey: HardwareKey?, - autofillComponent: AutofillComponent, - searchInfo: SearchInfo?) { - buildAndLaunchIntent(activity, databaseFile, keyFile, hardwareKey) { intent -> - EntrySelectionHelper.startActivityForAutofillSelectionModeResult( - activity, - intent, - activityResultLauncher, - autofillComponent, - searchInfo) - } - } - - /* - * ------------------------- - * Passkey Launch - * ------------------------- - */ - @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) - @Throws(FileNotFoundException::class) - fun launchForPasskeyResult(activity: Activity, - activityResultLauncher: ActivityResultLauncher?, - databaseFile: Uri, - keyFile: Uri?, - hardwareKey: HardwareKey?, - searchInfo: SearchInfo?) { - buildAndLaunchIntent(activity, databaseFile, keyFile, hardwareKey) { intent -> - EntrySelectionHelper.startActivityForPasskeySelectionModeResult( - activity, - intent, - activityResultLauncher, - searchInfo + context = activity, + intent = intent, + searchInfo = searchInfo ) } } /* * ------------------------- - * Registration Launch + * Selection Launch * ------------------------- */ + + @Throws(FileNotFoundException::class) + fun launchForSelection( + activity: AppCompatActivity, + databaseFile: Uri, + keyFile: Uri?, + hardwareKey: HardwareKey?, + typeMode: TypeMode, + searchInfo: SearchInfo?, + autofillComponent: AutofillComponent? = null, + activityResultLauncher: ActivityResultLauncher? = null, + ) { + buildAndLaunchIntent(activity, databaseFile, keyFile, hardwareKey) { intent -> + EntrySelectionHelper.startActivityForSelectionModeResult( + context = activity, + intent = intent, + typeMode = typeMode, + searchInfo = searchInfo, + autofillComponent = autofillComponent, + activityResultLauncher = activityResultLauncher + ) + } + } + + /* + * ------------------------- + * Registration Launch + * ------------------------- + */ + + @Throws(FileNotFoundException::class) fun launchForRegistration( activity: Activity, activityResultLauncher: ActivityResultLauncher?, @@ -888,123 +856,5 @@ class MainCredentialActivity : DatabaseModeActivity() { ) } } - - /* - * ------------------------- - * Global Launch - * ------------------------- - */ - fun launch(activity: AppCompatActivity, - databaseUri: Uri, - keyFile: Uri?, - hardwareKey: HardwareKey?, - fileNoFoundAction: (exception: FileNotFoundException) -> Unit, - onCancelSpecialMode: () -> Unit, - onLaunchActivitySpecialMode: () -> Unit, - activityResultLauncher: ActivityResultLauncher?) { - - try { - EntrySelectionHelper.doSpecialAction( - intent = activity.intent, - defaultAction = { - launch( - activity = activity, - databaseFile = databaseUri, - keyFile = keyFile, - hardwareKey = hardwareKey - ) - }, - searchAction = { searchInfo -> - launchForSearchResult( - activity = activity, - databaseFile = databaseUri, - keyFile = keyFile, - hardwareKey = hardwareKey, - searchInfo = searchInfo - ) - onLaunchActivitySpecialMode() - }, - registrationAction = { registerInfo -> - launchForRegistration( - activity = activity, - activityResultLauncher = activityResultLauncher, - databaseFile = databaseUri, - keyFile = keyFile, - hardwareKey = hardwareKey, - typeMode = TypeMode.DEFAULT, - registerInfo = registerInfo - ) - onLaunchActivitySpecialMode() - }, - keyboardSelectionAction = { searchInfo -> - launchForKeyboardResult( - activity = activity, - databaseFile = databaseUri, - keyFile = keyFile, - hardwareKey = hardwareKey, - searchInfo = searchInfo - ) - onLaunchActivitySpecialMode() - }, - autofillSelectionAction = { searchInfo, autofillComponent -> - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - launchForAutofillResult( - activity = activity, - activityResultLauncher = activityResultLauncher, - databaseFile = databaseUri, - keyFile = keyFile, - hardwareKey = hardwareKey, - autofillComponent = autofillComponent, - searchInfo = searchInfo - ) - onLaunchActivitySpecialMode() - } else { - onCancelSpecialMode() - } - }, - autofillRegistrationAction = { registerInfo -> - launchForRegistration( - activity = activity, - activityResultLauncher = activityResultLauncher, - databaseFile = databaseUri, - keyFile = keyFile, - hardwareKey = hardwareKey, - typeMode = TypeMode.AUTOFILL, - registerInfo = registerInfo - ) - onLaunchActivitySpecialMode() - }, - passkeySelectionAction = { searchInfo -> - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { - launchForPasskeyResult( - activity = activity, - activityResultLauncher = activityResultLauncher, - databaseFile = databaseUri, - keyFile = keyFile, - hardwareKey = hardwareKey, - searchInfo = searchInfo - ) - onLaunchActivitySpecialMode() - } else { - onCancelSpecialMode() - } - }, - passkeyRegistrationAction = { registerInfo -> - launchForRegistration( - activity = activity, - activityResultLauncher = activityResultLauncher, - databaseFile = databaseUri, - keyFile = keyFile, - hardwareKey = hardwareKey, - typeMode = TypeMode.PASSKEY, - registerInfo = registerInfo - ) - onLaunchActivitySpecialMode() - } - ) - } catch (e: FileNotFoundException) { - fileNoFoundAction(e) - } - } } } diff --git a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/EntrySelectionHelper.kt b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/EntrySelectionHelper.kt index 22623e8c4..e63c13829 100644 --- a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/EntrySelectionHelper.kt +++ b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/EntrySelectionHelper.kt @@ -102,58 +102,41 @@ object EntrySelectionHelper { } } - fun startActivityForSearchModeResult(context: Context, - intent: Intent, - searchInfo: SearchInfo) { + fun startActivityForSearchModeResult( + context: Context, + intent: Intent, + searchInfo: SearchInfo + ) { addSpecialModeInIntent(intent, SpecialMode.SEARCH) addSearchInfoInIntent(intent, searchInfo) intent.flags = intent.flags or Intent.FLAG_ACTIVITY_CLEAR_TASK context.startActivity(intent) } - fun startActivityForKeyboardSelectionModeResult(context: Context, - intent: Intent, - searchInfo: SearchInfo?) { - addSpecialModeInIntent(intent, SpecialMode.SELECTION) - addTypeModeInIntent(intent, TypeMode.MAGIKEYBOARD) - addSearchInfoInIntent(intent, searchInfo) - intent.flags = intent.flags or Intent.FLAG_ACTIVITY_CLEAR_TASK - context.startActivity(intent) - } - - /** - * Utility method to start an activity with an Autofill for result - */ - @RequiresApi(Build.VERSION_CODES.O) - fun startActivityForAutofillSelectionModeResult( + fun startActivityForSelectionModeResult( context: Context, intent: Intent, - activityResultLauncher: ActivityResultLauncher?, - autofillComponent: AutofillComponent, - searchInfo: SearchInfo? + typeMode: TypeMode, + searchInfo: SearchInfo?, + autofillComponent: AutofillComponent? = null, + activityResultLauncher: ActivityResultLauncher? = null, ) { addSpecialModeInIntent(intent, SpecialMode.SELECTION) - addTypeModeInIntent(intent, TypeMode.AUTOFILL) - intent.addAutofillComponent(context, autofillComponent) + addTypeModeInIntent(intent, typeMode) addSearchInfoInIntent(intent, searchInfo) - activityResultLauncher?.launch(intent) - } - - @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) - fun startActivityForPasskeySelectionModeResult( - context: Context, - intent: Intent, - activityResultLauncher: ActivityResultLauncher?, - searchInfo: SearchInfo? - ) { - addSpecialModeInIntent(intent, SpecialMode.SELECTION) - addTypeModeInIntent(intent, TypeMode.PASSKEY) - addSearchInfoInIntent(intent, searchInfo) - activityResultLauncher?.launch(intent) + autofillComponent?.let { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + intent.addAutofillComponent(context, autofillComponent) + } + } + if (activityResultLauncher == null) { + intent.flags = intent.flags or Intent.FLAG_ACTIVITY_CLEAR_TASK + } + activityResultLauncher?.launch(intent) ?: context.startActivity(intent) } fun startActivityForRegistrationModeResult( - context: Context?, + context: Context, activityResultLauncher: ActivityResultLauncher?, intent: Intent, registerInfo: RegisterInfo?, @@ -165,8 +148,7 @@ object EntrySelectionHelper { if (activityResultLauncher == null) { intent.flags = intent.flags or Intent.FLAG_ACTIVITY_CLEAR_TASK } - activityResultLauncher?.launch(intent) ?: context?.startActivity(intent) ?: - throw IllegalStateException("At least Context or ActivityResultLauncher must not be null") + activityResultLauncher?.launch(intent) ?: context.startActivity(intent) } fun addSearchInfoInIntent(intent: Intent, searchInfo: SearchInfo?) { @@ -293,7 +275,11 @@ object EntrySelectionHelper { defaultAction.invoke() } TypeMode.MAGIKEYBOARD -> keyboardSelectionAction.invoke(searchInfo) - TypeMode.PASSKEY -> passkeySelectionAction.invoke(searchInfo) + TypeMode.PASSKEY -> + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + passkeySelectionAction.invoke(searchInfo) + } else + defaultAction.invoke() else -> { // In this case, error removeModesFromIntent(intent) diff --git a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/AutofillLauncherActivity.kt b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/AutofillLauncherActivity.kt index bfe49cec6..4cdcac6ee 100644 --- a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/AutofillLauncherActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/AutofillLauncherActivity.kt @@ -145,22 +145,24 @@ class AutofillLauncherActivity : DatabaseModeActivity() { }, onItemNotFound = { openedDatabase -> // Show the database UI to select the entry - GroupActivity.launchForAutofillSelectionResult( - this, - openedDatabase, - mCredentialActivityResultLauncher, - autofillComponent, - searchInfo, - false + GroupActivity.launchForSelection( + context = this, + database = openedDatabase, + typeMode = TypeMode.AUTOFILL, + searchInfo = searchInfo, + autoSearch = false, + autofillComponent = autofillComponent, + activityResultLauncher = mCredentialActivityResultLauncher, ) }, onDatabaseClosed = { // If database not open - FileDatabaseSelectActivity.launchForAutofillResult( - this, - mCredentialActivityResultLauncher, - autofillComponent, - searchInfo + FileDatabaseSelectActivity.launchForSelection( + activity = this, + typeMode = TypeMode.AUTOFILL, + searchInfo = searchInfo, + autofillComponent = autofillComponent, + activityResultLauncher = mCredentialActivityResultLauncher ) } ) diff --git a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/EntrySelectionLauncherActivity.kt b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/EntrySelectionLauncherActivity.kt index 075c67b83..cbcee2d74 100644 --- a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/EntrySelectionLauncherActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/EntrySelectionLauncherActivity.kt @@ -152,11 +152,12 @@ class EntrySelectionLauncherActivity : DatabaseModeActivity() { ) }, { autoSearch -> - GroupActivity.launchForKeyboardSelectionResult( - this, - openedDatabase, - searchInfo, - autoSearch + GroupActivity.launchForSelection( + context = this, + database = openedDatabase, + typeMode = TypeMode.MAGIKEYBOARD, + searchInfo = searchInfo, + autoSearch = autoSearch ) } ) @@ -184,11 +185,12 @@ class EntrySelectionLauncherActivity : DatabaseModeActivity() { toastError(RegisterInReadOnlyDatabaseException()) } } else if (searchShareForMagikeyboard) { - GroupActivity.launchForKeyboardSelectionResult( - this, - openedDatabase, - searchInfo, - false + GroupActivity.launchForSelection( + context = this, + database = openedDatabase, + typeMode = TypeMode.MAGIKEYBOARD, + searchInfo = searchInfo, + autoSearch = false ) } else { GroupActivity.launchForSearchResult( @@ -209,9 +211,10 @@ class EntrySelectionLauncherActivity : DatabaseModeActivity() { typeMode = TypeMode.DEFAULT ) } else if (searchShareForMagikeyboard) { - FileDatabaseSelectActivity.launchForKeyboardSelectionResult( - this, - searchInfo + FileDatabaseSelectActivity.launchForSelection( + activity = this, + typeMode = TypeMode.MAGIKEYBOARD, + searchInfo = searchInfo ) } else { FileDatabaseSelectActivity.launchForSearchResult( diff --git a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/PasskeyLauncherActivity.kt b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/PasskeyLauncherActivity.kt index 0ff8b2a5d..7f6f99c1e 100644 --- a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/PasskeyLauncherActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/PasskeyLauncherActivity.kt @@ -118,9 +118,10 @@ class PasskeyLauncherActivity : DatabaseLockActivity() { passkeyLauncherViewModel.cancelResult() } is PasskeyLauncherViewModel.UIState.LaunchGroupActivityForSelection -> { - GroupActivity.launchForPasskeySelectionResult( + GroupActivity.launchForSelection( context = this@PasskeyLauncherActivity, database = uiState.database, + typeMode = TypeMode.PASSKEY, activityResultLauncher = mPasskeySelectionActivityResultLauncher, searchInfo = null, autoSearch = false @@ -136,8 +137,9 @@ class PasskeyLauncherActivity : DatabaseLockActivity() { ) } is PasskeyLauncherViewModel.UIState.LaunchFileDatabaseSelectActivityForSelection -> { - FileDatabaseSelectActivity.launchForPasskeySelectionResult( + FileDatabaseSelectActivity.launchForSelection( activity = this@PasskeyLauncherActivity, + typeMode = TypeMode.PASSKEY, activityResultLauncher = mPasskeySelectionActivityResultLauncher, searchInfo = uiState.searchInfo, )