Encapsulate special mode view and add blocking menu for autofill

This commit is contained in:
J-Jamet committed 2020-06-17 21:29:28 +02:00
1 parent 0c61f0ded2
commit c7e46205b3
7 files changed
+102 -67

No files matched your search

@@ -55,7 +55,6 @@ import com.kunzisoft.keepass.model.SearchInfo
import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_CREATE_TASK
import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.utils.*
import com.kunzisoft.keepass.view.SpecialModeView
import com.kunzisoft.keepass.view.asError
import kotlinx.android.synthetic.main.activity_file_selection.*
import java.io.FileNotFoundException
@@ -69,7 +68,6 @@ class FileDatabaseSelectActivity : SpecialModeActivity(),
private var databaseButtonsContainerView: View? = null
private var createDatabaseButtonView: View? = null
private var openDatabaseButtonView: View? = null
private var specialModeView: SpecialModeView? = null
// Adapter to manage database history list
private var mAdapterDatabaseHistory: FileDatabaseHistoryAdapter? = null
@@ -115,9 +113,6 @@ class FileDatabaseSelectActivity : SpecialModeActivity(),
}
}
// Special mode view
specialModeView = findViewById(R.id.special_mode_view)
// History list
val fileDatabaseHistoryRecyclerView = findViewById<RecyclerView>(R.id.file_list)
fileDatabaseHistoryRecyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
@@ -274,15 +269,6 @@ class FileDatabaseSelectActivity : SpecialModeActivity(),
override fun onResume() {
super.onResume()
// To show the selection mode
specialModeView?.apply {
searchInfo = intent.getParcelableExtra(KEY_SEARCH_INFO)
visible = mSelectionMode
onCancelButtonClickListener = View.OnClickListener {
onBackPressed()
}
}
// Show open and create button or special mode
if (mSelectionMode) {
// Disable buttons if in selection mode or request for autofill
@@ -94,7 +94,6 @@ class GroupActivity : LockingActivity(),
private var toolbarAction: ToolbarAction? = null
private var iconView: ImageView? = null
private var numberChildrenView: TextView? = null
private var specialModeView: SpecialModeView? = null
private var addNodeButtonView: AddNodeButtonView? = null
private var groupNameView: TextView? = null
@@ -136,7 +135,6 @@ class GroupActivity : LockingActivity(),
searchTitleView = findViewById(R.id.search_title)
groupNameView = findViewById(R.id.group_name)
toolbarAction = findViewById(R.id.toolbar_action)
specialModeView = findViewById(R.id.special_mode_view)
lockView = findViewById(R.id.lock_button)
lockView?.setOnClickListener {
@@ -463,25 +461,6 @@ class GroupActivity : LockingActivity(),
// Assign number of children
refreshNumberOfChildren()
// Show selection mode message if needed
specialModeView?.apply {
searchInfo = intent.getParcelableExtra(KEY_SEARCH_INFO)
visible = mSelectionMode
onCancelButtonClickListener = View.OnClickListener {
// To remove the navigation history and
EntrySelectionHelper.removeEntrySelectionModeFromIntent(intent)
val fragmentManager = supportFragmentManager
if (mSelectionModeCountBackStack > 0) {
for (selectionMode in 0 .. mSelectionModeCountBackStack) {
fragmentManager.popBackStack()
}
}
// Reinit the counter for navigation history
mSelectionModeCountBackStack = 0
backToTheAppCaller()
}
}
// Show button if allowed
addNodeButtonView?.apply {
@@ -499,6 +478,20 @@ class GroupActivity : LockingActivity(),
}
}
override fun onCancelSpecialMode() {
// To remove the navigation history and
EntrySelectionHelper.removeEntrySelectionModeFromIntent(intent)
val fragmentManager = supportFragmentManager
if (mSelectionModeCountBackStack > 0) {
for (selectionMode in 0 .. mSelectionModeCountBackStack) {
fragmentManager.popBackStack()
}
}
// Reinit the counter for navigation history
mSelectionModeCountBackStack = 0
backToTheAppCaller()
}
private fun refreshNumberOfChildren() {
numberChildrenView?.apply {
if (PreferencesUtil.showNumberEntries(context)) {
@@ -69,7 +69,6 @@ import com.kunzisoft.keepass.utils.MenuUtil
import com.kunzisoft.keepass.utils.UriUtil
import com.kunzisoft.keepass.view.AdvancedUnlockInfoView
import com.kunzisoft.keepass.view.KeyFileSelectionView
import com.kunzisoft.keepass.view.SpecialModeView
import com.kunzisoft.keepass.view.asError
import kotlinx.android.synthetic.main.activity_password.*
import java.io.FileNotFoundException
@@ -78,7 +77,6 @@ open class PasswordActivity : SpecialModeActivity() {
// Views
private var toolbar: Toolbar? = null
private var specialModeView: SpecialModeView? = null
private var filenameView: TextView? = null
private var passwordView: EditText? = null
private var keyFileSelectionView: KeyFileSelectionView? = null
@@ -125,7 +123,6 @@ open class PasswordActivity : SpecialModeActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)
specialModeView = findViewById(R.id.special_mode_view)
confirmButtonView = findViewById(R.id.activity_password_open_button)
filenameView = findViewById(R.id.filename)
passwordView = findViewById(R.id.password)
@@ -345,15 +342,6 @@ open class PasswordActivity : SpecialModeActivity() {
override fun onResume() {
super.onResume()
// To show the selection mode
specialModeView?.apply {
searchInfo = intent.getParcelableExtra(KEY_SEARCH_INFO)
visible = mSelectionMode
onCancelButtonClickListener = View.OnClickListener {
onBackPressed()
}
}
if (Database.getInstance().loaded) {
launchGroupActivity()
} else {
@@ -1,9 +1,14 @@
package com.kunzisoft.keepass.activities.selection
import android.os.Build
import android.view.View
import android.widget.Toast
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.helpers.EntrySelectionHelper
import com.kunzisoft.keepass.activities.stylish.StylishActivity
import com.kunzisoft.keepass.autofill.AutofillHelper
import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.view.SpecialModeView
/**
* Activity to manage special mode (ie: selection mode)
@@ -14,6 +19,12 @@ abstract class SpecialModeActivity : StylishActivity() {
protected var mAutofillSelection: Boolean = false
private var specialModeView: SpecialModeView? = null
open fun onCancelSpecialMode() {
onBackPressed()
}
override fun onResume() {
super.onResume()
@@ -21,5 +32,39 @@ abstract class SpecialModeActivity : StylishActivity() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mAutofillSelection = AutofillHelper.retrieveAssistStructure(intent) != null
}
// To show the selection mode
specialModeView = findViewById(R.id.special_mode_view)
specialModeView?.apply {
searchInfo = intent.getParcelableExtra(EntrySelectionHelper.KEY_SEARCH_INFO)
visible = mSelectionMode
onCancelButtonClickListener = View.OnClickListener {
onCancelSpecialMode()
}
menu.clear()
if (mAutofillSelection) {
menuInflater.inflate(R.menu.autofill, menu)
setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) {
R.id.menu_autofill -> {
val webDomain = searchInfo?.webDomain
val applicationId = searchInfo?.applicationId
if (webDomain != null) {
PreferencesUtil.addWebDomainToBlocklist(this@SpecialModeActivity,
webDomain)
} else if (applicationId != null) {
PreferencesUtil.addApplicationIdToBlocklist(this@SpecialModeActivity,
applicationId)
}
onCancelSpecialMode()
Toast.makeText(this@SpecialModeActivity,
R.string.autofill_block_restart,
Toast.LENGTH_LONG).show()
}
}
true
}
}
}
}
}
@@ -119,29 +119,33 @@ object AutofillHelper {
* Build the Autofill response for many entry
*/
fun buildResponse(activity: Activity, entriesInfo: List<EntryInfo>) {
var setResultOk = false
activity.intent?.extras?.let { extras ->
if (extras.containsKey(ASSIST_STRUCTURE)) {
activity.intent?.getParcelableExtra<AssistStructure>(ASSIST_STRUCTURE)?.let { structure ->
StructureParser(structure).parse()?.let { result ->
// New Response
val responseBuilder = FillResponse.Builder()
entriesInfo.forEach {
responseBuilder.addDataset(buildDataset(activity, it, result))
if (entriesInfo.isEmpty()) {
activity.setResult(Activity.RESULT_CANCELED)
} else {
var setResultOk = false
activity.intent?.extras?.let { extras ->
if (extras.containsKey(ASSIST_STRUCTURE)) {
activity.intent?.getParcelableExtra<AssistStructure>(ASSIST_STRUCTURE)?.let { structure ->
StructureParser(structure).parse()?.let { result ->
// New Response
val responseBuilder = FillResponse.Builder()
entriesInfo.forEach {
responseBuilder.addDataset(buildDataset(activity, it, result))
}
val mReplyIntent = Intent()
Log.d(activity.javaClass.name, "Successed Autofill auth.")
mReplyIntent.putExtra(
AutofillManager.EXTRA_AUTHENTICATION_RESULT,
responseBuilder.build())
setResultOk = true
activity.setResult(Activity.RESULT_OK, mReplyIntent)
}
val mReplyIntent = Intent()
Log.d(activity.javaClass.name, "Successed Autofill auth.")
mReplyIntent.putExtra(
AutofillManager.EXTRA_AUTHENTICATION_RESULT,
responseBuilder.build())
setResultOk = true
activity.setResult(Activity.RESULT_OK, mReplyIntent)
}
}
}
if (!setResultOk) {
Log.w(activity.javaClass.name, "Failed Autofill auth.")
activity.setResult(Activity.RESULT_CANCELED)
if (!setResultOk) {
Log.w(activity.javaClass.name, "Failed Autofill auth.")
activity.setResult(Activity.RESULT_CANCELED)
}
}
}
}
@@ -386,4 +386,22 @@ object PreferencesUtil {
context.resources.getStringArray(R.array.autofill_web_domain_blocklist_default).toMutableSet())
?: emptySet()
}
fun addApplicationIdToBlocklist(context: Context, applicationId: String) {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
val setItems: MutableSet<String> = applicationIdBlocklist(context).toMutableSet()
setItems.add(applicationId)
prefs.edit()
.putStringSet(context.getString(R.string.autofill_application_id_blocklist_key), setItems)
.apply()
}
fun addWebDomainToBlocklist(context: Context, webDomain: String) {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
val setItems: MutableSet<String> = webDomainBlocklist(context).toMutableSet()
setItems.add(webDomain)
prefs.edit()
.putStringSet(context.getString(R.string.autofill_web_domain_blocklist_key), setItems)
.apply()
}
}