Better readOnly implementation

This commit is contained in:
J-Jamet
2021-08-14 12:37:20 +02:00
parent 3026a9e3e4
commit 7593a05953
16 changed files with 67 additions and 204 deletions

View File

@@ -117,7 +117,6 @@ class AutofillLauncherActivity : DatabaseModeActivity() {
{
// Show the database UI to select the entry
GroupActivity.launchForAutofillResult(this,
database?.isReadOnly != false,
autofillComponent,
searchInfo,
false)

View File

@@ -39,7 +39,6 @@ import com.google.android.material.appbar.CollapsingToolbarLayout
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.fragments.EntryFragment
import com.kunzisoft.keepass.activities.helpers.ExternalFileHelper
import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper
import com.kunzisoft.keepass.activities.helpers.SpecialMode
import com.kunzisoft.keepass.activities.legacy.LockingActivity
import com.kunzisoft.keepass.database.element.Attachment
@@ -219,8 +218,7 @@ class EntryActivity : LockingActivity() {
mEntryViewModel.historySelected.observe(this) { historySelected ->
launch(this,
historySelected.nodeId,
historySelected.historyPosition,
mReadOnly)
historySelected.historyPosition)
}
}
@@ -322,10 +320,10 @@ class EntryActivity : LockingActivity() {
menu.findItem(R.id.menu_goto_url)?.isVisible = false
}
if (mEntryIsHistory && !mReadOnly) {
if (mEntryIsHistory && !mDatabaseReadOnly) {
inflater.inflate(R.menu.entry_history, menu)
}
if (mEntryIsHistory || mReadOnly) {
if (mEntryIsHistory || mDatabaseReadOnly) {
menu.findItem(R.id.menu_save_database)?.isVisible = false
menu.findItem(R.id.menu_edit)?.isVisible = false
}
@@ -432,11 +430,10 @@ class EntryActivity : LockingActivity() {
/**
* Open standard Entry activity
*/
fun launch(activity: Activity, entryId: NodeId<UUID>, readOnly: Boolean) {
fun launch(activity: Activity, entryId: NodeId<UUID>) {
if (TimeoutHelper.checkTimeAndLockIfTimeout(activity)) {
val intent = Intent(activity, EntryActivity::class.java)
intent.putExtra(KEY_ENTRY, entryId)
ReadOnlyHelper.putReadOnlyInIntent(intent, readOnly)
activity.startActivityForResult(intent, EntryEditActivity.ADD_OR_UPDATE_ENTRY_REQUEST_CODE)
}
}
@@ -444,12 +441,11 @@ class EntryActivity : LockingActivity() {
/**
* Open history Entry activity
*/
fun launch(activity: Activity, entryId: NodeId<UUID>, historyPosition: Int, readOnly: Boolean) {
fun launch(activity: Activity, entryId: NodeId<UUID>, historyPosition: Int) {
if (TimeoutHelper.checkTimeAndLockIfTimeout(activity)) {
val intent = Intent(activity, EntryActivity::class.java)
intent.putExtra(KEY_ENTRY, entryId)
intent.putExtra(KEY_ENTRY_HISTORY_POSITION, historyPosition)
ReadOnlyHelper.putReadOnlyInIntent(intent, readOnly)
activity.startActivityForResult(intent, EntryEditActivity.ADD_OR_UPDATE_ENTRY_REQUEST_CODE)
}
}

View File

@@ -679,7 +679,7 @@ class EntryEditActivity : LockingActivity(),
const val ADD_OR_UPDATE_ENTRY_REQUEST_CODE = 7129
const val ADD_OR_UPDATE_ENTRY_KEY = "ADD_OR_UPDATE_ENTRY_KEY"
const val ENTRY_EDIT_FRAGMENT_TAG = "ENTRY_EDIT_FRAGMENT_TAG"
// TODO Only not readonly
/**
* Launch EntryEditActivity to update an existing entry

View File

@@ -122,13 +122,11 @@ class EntrySelectionLauncherActivity : DatabaseModeActivity() {
} else {
// Select the one we want
GroupActivity.launchForKeyboardSelectionResult(this,
readOnly,
searchInfo,
true)
}
} else {
GroupActivity.launchForSearchResult(this,
readOnly,
searchInfo,
true)
}
@@ -148,7 +146,6 @@ class EntrySelectionLauncherActivity : DatabaseModeActivity() {
}
} else if (readOnly || searchShareForMagikeyboard) {
GroupActivity.launchForKeyboardSelectionResult(this,
readOnly,
searchInfo,
false)
} else {

View File

@@ -268,7 +268,6 @@ class FileDatabaseSelectActivity : DatabaseModeActivity(),
private fun launchGroupActivity(database: Database) {
GroupActivity.launch(this,
database,
database.isReadOnly,
{ onValidateSpecialMode() },
{ onCancelSpecialMode() },
{ onLaunchActivitySpecialMode() })

View File

@@ -43,7 +43,6 @@ import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.dialogs.*
import com.kunzisoft.keepass.activities.fragments.GroupFragment
import com.kunzisoft.keepass.activities.helpers.EntrySelectionHelper
import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper
import com.kunzisoft.keepass.activities.helpers.SpecialMode
import com.kunzisoft.keepass.activities.legacy.LockingActivity
import com.kunzisoft.keepass.adapters.SearchEntryCursorAdapter
@@ -320,7 +319,7 @@ class GroupActivity : LockingActivity(),
mGroupViewModel.setDatabase(database)
mGroupEditViewModel.setGroupNamesNotAllowed(database?.groupNamesNotAllowed)
mRecyclingBinEnabled = !mReadOnly
mRecyclingBinEnabled = !mDatabaseReadOnly
&& database?.isRecycleBinEnabled == true
mRootGroup = database?.rootGroup
@@ -529,8 +528,8 @@ class GroupActivity : LockingActivity(),
addNodeButtonView?.apply {
closeButtonIfOpen()
// To enable add button
val addGroupEnabled = !mReadOnly && group?.isVirtual != true
var addEntryEnabled = !mReadOnly && group?.isVirtual != true
val addGroupEnabled = !mDatabaseReadOnly && group?.isVirtual != true
var addEntryEnabled = !mDatabaseReadOnly && group?.isVirtual != true
group?.let {
if (!it.allowAddEntryIfIsRoot)
addEntryEnabled = it != mRootGroup && addEntryEnabled
@@ -585,13 +584,13 @@ class GroupActivity : LockingActivity(),
val entryVersioned = node as Entry
EntrySelectionHelper.doSpecialAction(intent,
{
EntryActivity.launch(this@GroupActivity, entryVersioned.nodeId, mReadOnly)
EntryActivity.launch(this@GroupActivity, entryVersioned.nodeId)
},
{
// Nothing here, a search is simply performed
},
{ searchInfo ->
if (!mReadOnly)
if (!database.isReadOnly)
entrySelectedForSave(entryVersioned, searchInfo)
else
finish()
@@ -607,7 +606,7 @@ class GroupActivity : LockingActivity(),
},
{
// Item not found, save it if required
if (!mReadOnly
if (!database.isReadOnly
&& searchInfo != null
&& PreferencesUtil.isKeyboardSaveSearchInfoEnable(this@GroupActivity)
) {
@@ -623,7 +622,7 @@ class GroupActivity : LockingActivity(),
)
},
{ searchInfo, _ ->
if (!mReadOnly
if (!database.isReadOnly
&& searchInfo != null
&& PreferencesUtil.isAutofillSaveSearchInfoEnable(this@GroupActivity)
) {
@@ -633,7 +632,7 @@ class GroupActivity : LockingActivity(),
}
},
{ registerInfo ->
if (!mReadOnly)
if (!database.isReadOnly)
entrySelectedForRegistration(entryVersioned, registerInfo)
else
finish()
@@ -856,7 +855,7 @@ class GroupActivity : LockingActivity(),
val inflater = menuInflater
inflater.inflate(R.menu.search, menu)
inflater.inflate(R.menu.database, menu)
if (mReadOnly) {
if (mDatabaseReadOnly) {
menu.findItem(R.id.menu_save_database)?.isVisible = false
}
if (mSpecialMode == SpecialMode.DEFAULT) {
@@ -999,7 +998,7 @@ class GroupActivity : LockingActivity(),
}
else -> {
// Check the time lock before launching settings
MenuUtil.onDefaultMenuOptionsItemSelected(this, item, mReadOnly, true)
MenuUtil.onDefaultMenuOptionsItemSelected(this, item, true)
return super.onOptionsItemSelected(item)
}
}
@@ -1154,31 +1153,27 @@ class GroupActivity : LockingActivity(),
private fun buildIntent(context: Context,
groupState: GroupState?,
readOnly: Boolean,
intentBuildLauncher: (Intent) -> Unit) {
val intent = Intent(context, GroupActivity::class.java)
if (groupState != null) {
intent.putExtra(GROUP_STATE_KEY, groupState)
}
ReadOnlyHelper.putReadOnlyInIntent(intent, readOnly)
intentBuildLauncher.invoke(intent)
}
private fun checkTimeAndBuildIntent(activity: Activity,
groupState: GroupState?,
readOnly: Boolean,
intentBuildLauncher: (Intent) -> Unit) {
if (TimeoutHelper.checkTimeAndLockIfTimeout(activity)) {
buildIntent(activity, groupState, readOnly, intentBuildLauncher)
buildIntent(activity, groupState, intentBuildLauncher)
}
}
private fun checkTimeAndBuildIntent(context: Context,
groupState: GroupState?,
readOnly: Boolean,
intentBuildLauncher: (Intent) -> Unit) {
if (TimeoutHelper.checkTime(context)) {
buildIntent(context, groupState, readOnly, intentBuildLauncher)
buildIntent(context, groupState, intentBuildLauncher)
}
}
@@ -1188,9 +1183,8 @@ class GroupActivity : LockingActivity(),
* -------------------------
*/
fun launch(context: Context,
readOnly: Boolean,
autoSearch: Boolean = false) {
checkTimeAndBuildIntent(context, null, readOnly) { intent ->
checkTimeAndBuildIntent(context, null) { intent ->
intent.putExtra(AUTO_SEARCH_KEY, autoSearch)
context.startActivity(intent)
}
@@ -1202,10 +1196,9 @@ class GroupActivity : LockingActivity(),
* -------------------------
*/
fun launchForSearchResult(context: Context,
readOnly: Boolean,
searchInfo: SearchInfo,
autoSearch: Boolean = false) {
checkTimeAndBuildIntent(context, null, readOnly) { intent ->
checkTimeAndBuildIntent(context, null) { intent ->
intent.putExtra(AUTO_SEARCH_KEY, autoSearch)
EntrySelectionHelper.addSearchInfoInIntent(
intent,
@@ -1222,7 +1215,8 @@ class GroupActivity : LockingActivity(),
fun launchForSaveResult(context: Context,
searchInfo: SearchInfo,
autoSearch: Boolean = false) {
checkTimeAndBuildIntent(context, null, false) { intent ->
// TODO Only not readonly
checkTimeAndBuildIntent(context, null) { intent ->
intent.putExtra(AUTO_SEARCH_KEY, autoSearch)
EntrySelectionHelper.startActivityForSaveModeResult(context,
intent,
@@ -1236,10 +1230,9 @@ class GroupActivity : LockingActivity(),
* -------------------------
*/
fun launchForKeyboardSelectionResult(context: Context,
readOnly: Boolean,
searchInfo: SearchInfo? = null,
autoSearch: Boolean = false) {
checkTimeAndBuildIntent(context, null, readOnly) { intent ->
checkTimeAndBuildIntent(context, null) { intent ->
intent.putExtra(AUTO_SEARCH_KEY, autoSearch)
EntrySelectionHelper.startActivityForKeyboardSelectionModeResult(context,
intent,
@@ -1254,11 +1247,10 @@ class GroupActivity : LockingActivity(),
*/
@RequiresApi(api = Build.VERSION_CODES.O)
fun launchForAutofillResult(activity: Activity,
readOnly: Boolean,
autofillComponent: AutofillComponent,
searchInfo: SearchInfo? = null,
autoSearch: Boolean = false) {
checkTimeAndBuildIntent(activity, null, readOnly) { intent ->
checkTimeAndBuildIntent(activity, null) { intent ->
intent.putExtra(AUTO_SEARCH_KEY, autoSearch)
AutofillHelper.startActivityForAutofillResult(activity,
intent,
@@ -1274,7 +1266,8 @@ class GroupActivity : LockingActivity(),
*/
fun launchForRegistration(context: Context,
registerInfo: RegisterInfo? = null) {
checkTimeAndBuildIntent(context, null, false) { intent ->
// TODO Only not readonly
checkTimeAndBuildIntent(context, null) { intent ->
intent.putExtra(AUTO_SEARCH_KEY, false)
EntrySelectionHelper.startActivityForRegistrationModeResult(context,
intent,
@@ -1289,14 +1282,12 @@ class GroupActivity : LockingActivity(),
*/
fun launch(activity: Activity,
database: Database,
readOnly: Boolean,
onValidateSpecialMode: () -> Unit,
onCancelSpecialMode: () -> Unit,
onLaunchActivitySpecialMode: () -> Unit) {
EntrySelectionHelper.doSpecialAction(activity.intent,
{
GroupActivity.launch(activity,
readOnly,
true)
},
{ searchInfo ->
@@ -1306,16 +1297,14 @@ class GroupActivity : LockingActivity(),
{ _, _ ->
// Response is build
GroupActivity.launchForSearchResult(activity,
readOnly,
searchInfo,
true)
onLaunchActivitySpecialMode()
},
{
// Here no search info found
if (readOnly) {
if (database.isReadOnly) {
GroupActivity.launchForSearchResult(activity,
readOnly,
searchInfo,
false)
} else {
@@ -1333,7 +1322,7 @@ class GroupActivity : LockingActivity(),
},
{ searchInfo ->
// Save info used with OTP
if (!readOnly) {
if (!database.isReadOnly) {
GroupActivity.launchForSaveResult(activity,
searchInfo,
false)
@@ -1360,7 +1349,6 @@ class GroupActivity : LockingActivity(),
} else {
// Select the one we want
GroupActivity.launchForKeyboardSelectionResult(activity,
readOnly,
searchInfo,
true)
onLaunchActivitySpecialMode()
@@ -1369,7 +1357,6 @@ class GroupActivity : LockingActivity(),
{
// Here no search info found, disable auto search
GroupActivity.launchForKeyboardSelectionResult(activity,
readOnly,
searchInfo,
false)
onLaunchActivitySpecialMode()
@@ -1393,7 +1380,6 @@ class GroupActivity : LockingActivity(),
{
// Here no search info found, disable auto search
GroupActivity.launchForAutofillResult(activity,
readOnly,
autofillComponent,
searchInfo,
false)
@@ -1409,7 +1395,7 @@ class GroupActivity : LockingActivity(),
}
},
{ registerInfo ->
if (!readOnly) {
if (!database.isReadOnly) {
SearchHelper.checkAutoSearchInfo(activity,
database,
registerInfo?.searchInfo,

View File

@@ -38,7 +38,6 @@ class MagikeyboardLauncherActivity : DatabaseModeActivity() {
override fun onDatabaseRetrieved(database: Database?) {
super.onDatabaseRetrieved(database)
val readOnly = database?.isReadOnly != false
SearchHelper.checkAutoSearchInfo(this,
database,
null,
@@ -48,7 +47,7 @@ class MagikeyboardLauncherActivity : DatabaseModeActivity() {
},
{
// Select if not found
GroupActivity.launchForKeyboardSelectionResult(this, readOnly)
GroupActivity.launchForKeyboardSelectionResult(this)
},
{
// Pass extra to get entry

View File

@@ -95,11 +95,11 @@ open class PasswordActivity : DatabaseModeActivity(), AdvancedUnlockFragment.Bui
private var mExternalFileHelper: ExternalFileHelper? = null
private var mPermissionAsked = false
private var readOnly: Boolean = false
private var mReadOnly: Boolean = false
private var mForceReadOnly: Boolean = false
set(value) {
infoContainerView?.visibility = if (value) {
readOnly = true
mReadOnly = true
View.VISIBLE
} else {
View.GONE
@@ -130,7 +130,11 @@ open class PasswordActivity : DatabaseModeActivity(), AdvancedUnlockFragment.Bui
coordinatorLayout = findViewById(R.id.activity_password_coordinator_layout)
mPermissionAsked = savedInstanceState?.getBoolean(KEY_PERMISSION_ASKED) ?: mPermissionAsked
readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrPreference(this, savedInstanceState)
mReadOnly = if (savedInstanceState != null && savedInstanceState.containsKey(KEY_READ_ONLY)) {
savedInstanceState.getBoolean(KEY_READ_ONLY)
} else {
PreferencesUtil.enableReadOnlyDatabase(this)
}
mRememberKeyFile = PreferencesUtil.rememberKeyFileLocations(this)
mExternalFileHelper = ExternalFileHelper(this@PasswordActivity)
@@ -332,7 +336,6 @@ open class PasswordActivity : DatabaseModeActivity(), AdvancedUnlockFragment.Bui
private fun launchGroupActivity(database: Database) {
GroupActivity.launch(this,
database,
readOnly,
{ onValidateSpecialMode() },
{ onCancelSpecialMode() },
{ onLaunchActivitySpecialMode() }
@@ -472,7 +475,7 @@ open class PasswordActivity : DatabaseModeActivity(), AdvancedUnlockFragment.Bui
mDatabaseKeyFileUri?.let {
outState.putString(KEY_KEYFILE, it.toString())
}
ReadOnlyHelper.onSaveInstanceState(outState, readOnly)
outState.putBoolean(KEY_READ_ONLY, mReadOnly)
outState.putBoolean(ALLOW_AUTO_OPEN_BIOMETRIC_PROMPT, false)
super.onSaveInstanceState(outState)
}
@@ -510,7 +513,7 @@ open class PasswordActivity : DatabaseModeActivity(), AdvancedUnlockFragment.Bui
clearCredentialsViews()
}
if (readOnly && (
if (mReadOnly && (
mSpecialMode == SpecialMode.SAVE
|| mSpecialMode == SpecialMode.REGISTRATION)
) {
@@ -524,7 +527,7 @@ open class PasswordActivity : DatabaseModeActivity(), AdvancedUnlockFragment.Bui
showProgressDialogAndLoadDatabase(
databaseUri,
MainCredential(password, keyFileUri),
readOnly,
mReadOnly,
cipherDatabaseEntity,
false)
}
@@ -575,7 +578,7 @@ open class PasswordActivity : DatabaseModeActivity(), AdvancedUnlockFragment.Bui
// Check permission
private fun checkPermission() {
if (Build.VERSION.SDK_INT in 23..28
&& !readOnly
&& !mReadOnly
&& !mPermissionAsked) {
mPermissionAsked = true
// Check self permission to show or not the dialog
@@ -652,7 +655,7 @@ open class PasswordActivity : DatabaseModeActivity(), AdvancedUnlockFragment.Bui
}
private fun changeOpenFileReadIcon(togglePassword: MenuItem) {
if (readOnly) {
if (mReadOnly) {
togglePassword.setTitle(R.string.menu_file_selection_read_only)
togglePassword.setIcon(R.drawable.ic_read_only_white_24dp)
} else {
@@ -666,7 +669,7 @@ open class PasswordActivity : DatabaseModeActivity(), AdvancedUnlockFragment.Bui
when (item.itemId) {
android.R.id.home -> finish()
R.id.menu_open_file_read_mode_key -> {
readOnly = !readOnly
mReadOnly = !mReadOnly
changeOpenFileReadIcon(item)
}
else -> MenuUtil.onDefaultMenuOptionsItemSelected(this, item)
@@ -724,6 +727,7 @@ open class PasswordActivity : DatabaseModeActivity(), AdvancedUnlockFragment.Bui
private const val KEY_KEYFILE = "keyFile"
private const val VIEW_INTENT = "android.intent.action.VIEW"
private const val KEY_READ_ONLY = "KEY_READ_ONLY"
private const val KEY_PASSWORD = "password"
private const val KEY_LAUNCH_IMMEDIATELY = "launchImmediately"
private const val KEY_PERMISSION_ASKED = "KEY_PERMISSION_ASKED"

View File

@@ -33,7 +33,6 @@ import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.EntryEditActivity
import com.kunzisoft.keepass.activities.dialogs.SortDialogFragment
import com.kunzisoft.keepass.activities.helpers.EntrySelectionHelper
import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper
import com.kunzisoft.keepass.activities.helpers.SpecialMode
import com.kunzisoft.keepass.adapters.NodeAdapter
import com.kunzisoft.keepass.database.element.Database
@@ -71,7 +70,6 @@ class GroupFragment : DatabaseFragment(), SortDialogFragment.SortSelectionListen
private var notFoundView: View? = null
private var isASearchResult: Boolean = false
private var readOnly: Boolean = false
private var specialMode: SpecialMode = SpecialMode.DEFAULT
private var mRecycleBinEnable: Boolean = false
@@ -123,8 +121,6 @@ class GroupFragment : DatabaseFragment(), SortDialogFragment.SortSelectionListen
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrArguments(savedInstanceState, arguments)
}
override fun onDatabaseRetrieved(database: Database?) {
@@ -275,11 +271,6 @@ class GroupFragment : DatabaseFragment(), SortDialogFragment.SortSelectionListen
super.onPause()
}
override fun onSaveInstanceState(outState: Bundle) {
ReadOnlyHelper.onSaveInstanceState(outState, readOnly)
super.onSaveInstanceState(outState)
}
fun getFirstVisiblePosition(): Int {
return mLayoutManager?.findFirstVisibleItemPosition() ?: 0
}
@@ -378,7 +369,7 @@ class GroupFragment : DatabaseFragment(), SortDialogFragment.SortSelectionListen
// Open and Edit for a single item
if (nodes.size == 1) {
// Edition
if (readOnly
if (database.isReadOnly
|| (mRecycleBinEnable && nodes[0] == mRecycleBin)) {
menu?.removeItem(R.id.menu_edit)
}
@@ -388,20 +379,20 @@ class GroupFragment : DatabaseFragment(), SortDialogFragment.SortSelectionListen
}
// Move
if (readOnly
if (database.isReadOnly
|| isASearchResult) {
menu?.removeItem(R.id.menu_move)
}
// Copy (not allowed for group)
if (readOnly
if (database.isReadOnly
|| isASearchResult
|| nodes.any { it.type == Type.GROUP }) {
menu?.removeItem(R.id.menu_copy)
}
// Deletion
if (readOnly
if (database.isReadOnly
|| (mRecycleBinEnable && nodes.any { it == mRecycleBin })) {
menu?.removeItem(R.id.menu_delete)
}

View File

@@ -1,79 +0,0 @@
/*
* Copyright 2019 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePassDX.
*
* KeePassDX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePassDX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.activities.helpers
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.kunzisoft.keepass.settings.PreferencesUtil
object ReadOnlyHelper {
private const val READ_ONLY_KEY = "READ_ONLY_KEY"
const val READ_ONLY_DEFAULT = false
fun retrieveReadOnlyFromIntent(intent: Intent): Boolean {
return intent.getBooleanExtra(READ_ONLY_KEY, READ_ONLY_DEFAULT)
}
fun retrieveReadOnlyFromInstanceStateOrPreference(context: Context, savedInstanceState: Bundle?): Boolean {
return if (savedInstanceState != null && savedInstanceState.containsKey(READ_ONLY_KEY)) {
savedInstanceState.getBoolean(READ_ONLY_KEY)
} else {
PreferencesUtil.enableReadOnlyDatabase(context)
}
}
// TODO remove read only
fun retrieveReadOnlyFromInstanceStateOrArguments(savedInstanceState: Bundle?, arguments: Bundle?): Boolean {
var readOnly = READ_ONLY_DEFAULT
if (savedInstanceState != null && savedInstanceState.containsKey(READ_ONLY_KEY)) {
readOnly = savedInstanceState.getBoolean(READ_ONLY_KEY)
} else if (arguments != null && arguments.containsKey(READ_ONLY_KEY)) {
readOnly = arguments.getBoolean(READ_ONLY_KEY)
}
return readOnly
}
fun retrieveReadOnlyFromInstanceStateOrIntent(savedInstanceState: Bundle?, intent: Intent?): Boolean {
var readOnly = READ_ONLY_DEFAULT
if (savedInstanceState != null && savedInstanceState.containsKey(READ_ONLY_KEY)) {
readOnly = savedInstanceState.getBoolean(READ_ONLY_KEY)
} else {
if (intent != null)
readOnly = intent.getBooleanExtra(READ_ONLY_KEY, READ_ONLY_DEFAULT)
}
return readOnly
}
fun putReadOnlyInIntent(intent: Intent, readOnly: Boolean) {
intent.putExtra(READ_ONLY_KEY, readOnly)
}
fun putReadOnlyInBundle(bundle: Bundle, readOnly: Boolean) {
bundle.putBoolean(READ_ONLY_KEY, readOnly)
}
fun onSaveInstanceState(outState: Bundle, readOnly: Boolean) {
outState.putBoolean(READ_ONLY_KEY, readOnly)
}
}

View File

@@ -33,7 +33,6 @@ import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.dialogs.DeleteNodesDialogFragment
import com.kunzisoft.keepass.activities.dialogs.PasswordEncodingDialogFragment
import com.kunzisoft.keepass.activities.helpers.EntrySelectionHelper
import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper
import com.kunzisoft.keepass.activities.helpers.SpecialMode
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.Entry
@@ -64,15 +63,7 @@ abstract class LockingActivity : DatabaseModeActivity(),
private var mDatabase: Database? = null
// Force readOnly if Entry Selection mode
protected var mReadOnly: Boolean
get() {
return mReadOnlyToSave
}
set(value) {
mReadOnlyToSave = value
}
private var mReadOnlyToSave: Boolean = false
protected var mDatabaseReadOnly: Boolean = true
private var mAutoSaveEnable: Boolean = true
protected var mIconDrawableFactory: IconDrawableFactory? = null
@@ -135,8 +126,7 @@ abstract class LockingActivity : DatabaseModeActivity(),
TimeoutHelper.recordTime(this, database.loaded)
}
// Force read only if the database is like that
mReadOnly = database.isReadOnly || mReadOnly
mDatabaseReadOnly = database.isReadOnly
mIconDrawableFactory = database.iconDrawableFactory
}
}
@@ -190,22 +180,22 @@ abstract class LockingActivity : DatabaseModeActivity(),
fun createEntry(newEntry: Entry,
parent: Group) {
createDatabaseEntry(newEntry, parent, !mReadOnly && mAutoSaveEnable)
createDatabaseEntry(newEntry, parent, !mDatabaseReadOnly && mAutoSaveEnable)
}
fun updateEntry(oldEntry: Entry,
entryToUpdate: Entry) {
updateDatabaseEntry(oldEntry, entryToUpdate, !mReadOnly && mAutoSaveEnable)
updateDatabaseEntry(oldEntry, entryToUpdate, !mDatabaseReadOnly && mAutoSaveEnable)
}
fun copyNodes(nodesToCopy: List<Node>,
newParent: Group) {
copyDatabaseNodes(nodesToCopy, newParent, !mReadOnly && mAutoSaveEnable)
copyDatabaseNodes(nodesToCopy, newParent, !mDatabaseReadOnly && mAutoSaveEnable)
}
fun moveNodes(nodesToMove: List<Node>,
newParent: Group) {
moveDatabaseNodes(nodesToMove, newParent, !mReadOnly && mAutoSaveEnable)
moveDatabaseNodes(nodesToMove, newParent, !mDatabaseReadOnly && mAutoSaveEnable)
}
private fun eachNodeRecyclable(database: Database, nodes: List<Node>): Boolean {
@@ -241,7 +231,7 @@ abstract class LockingActivity : DatabaseModeActivity(),
}
private fun permanentlyDeleteNodes(nodes: List<Node>) {
deleteDatabaseNodes(nodes,!mReadOnly && mAutoSaveEnable)
deleteDatabaseNodes(nodes,!mDatabaseReadOnly && mAutoSaveEnable)
}
fun createGroup(parent: Group,
@@ -253,7 +243,7 @@ abstract class LockingActivity : DatabaseModeActivity(),
}
// Not really needed here because added in runnable but safe
newGroup.parent = parent
createDatabaseGroup(newGroup, parent, !mReadOnly && mAutoSaveEnable)
createDatabaseGroup(newGroup, parent, !mDatabaseReadOnly && mAutoSaveEnable)
}
}
@@ -268,17 +258,17 @@ abstract class LockingActivity : DatabaseModeActivity(),
this.setGroupInfo(groupInfo)
}
}
updateDatabaseGroup(oldGroup, updateGroup, !mReadOnly && mAutoSaveEnable)
updateDatabaseGroup(oldGroup, updateGroup, !mDatabaseReadOnly && mAutoSaveEnable)
}
fun restoreEntryHistory(mainEntryId: NodeId<UUID>,
entryHistoryPosition: Int) {
restoreDatabaseEntryHistory(mainEntryId, entryHistoryPosition, !mReadOnly && mAutoSaveEnable)
restoreDatabaseEntryHistory(mainEntryId, entryHistoryPosition, !mDatabaseReadOnly && mAutoSaveEnable)
}
fun deleteEntryHistory(mainEntryId: NodeId<UUID>,
entryHistoryPosition: Int) {
deleteDatabaseEntryHistory(mainEntryId, entryHistoryPosition, !mReadOnly && mAutoSaveEnable)
deleteDatabaseEntryHistory(mainEntryId, entryHistoryPosition, !mDatabaseReadOnly && mAutoSaveEnable)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
@@ -295,14 +285,13 @@ abstract class LockingActivity : DatabaseModeActivity(),
// If in ave or registration mode, don't allow read only
if ((mSpecialMode == SpecialMode.SAVE
|| mSpecialMode == SpecialMode.REGISTRATION)
&& mReadOnly) {
&& mDatabaseReadOnly) {
Toast.makeText(this, R.string.error_registration_read_only , Toast.LENGTH_LONG).show()
EntrySelectionHelper.removeModesFromIntent(intent)
finish()
}
// To refresh when back to normal workflow from selection workflow
mReadOnlyToSave = ReadOnlyHelper.retrieveReadOnlyFromIntent(intent)
mAutoSaveEnable = PreferencesUtil.isAutoSaveDatabaseEnabled(this)
// Invalidate timeout by touch

View File

@@ -26,7 +26,6 @@ import android.os.*
import android.util.Log
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.GroupActivity
import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper
import com.kunzisoft.keepass.app.database.CipherDatabaseEntity
import com.kunzisoft.keepass.database.action.*
import com.kunzisoft.keepass.database.action.history.DeleteEntryHistoryDatabaseRunnable
@@ -398,9 +397,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
val pendingDatabaseIntent = PendingIntent.getActivity(
this,
0,
Intent(this, GroupActivity::class.java).apply {
ReadOnlyHelper.putReadOnlyInIntent(this, database.isReadOnly)
},
Intent(this, GroupActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT
)
val pendingDeleteIntent = PendingIntent.getBroadcast(

View File

@@ -32,7 +32,6 @@ import com.kunzisoft.androidclearchroma.ChromaUtil
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.DatabaseRetrieval
import com.kunzisoft.keepass.activities.dialogs.AssignMasterKeyDialogFragment
import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper
import com.kunzisoft.keepass.activities.legacy.resetAppTimeoutWhenViewFocusedOrChanged
import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm
import com.kunzisoft.keepass.database.crypto.kdf.KdfEngine
@@ -87,9 +86,6 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment(), DatabaseRetriev
override fun onCreateScreenPreference(screen: Screen, savedInstanceState: Bundle?, rootKey: String?) {
setHasOptionsMenu(true)
// TODO Read only
mDatabaseReadOnly = mDatabaseReadOnly || ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrArguments(savedInstanceState, arguments)
mScreen = screen
val database = mDatabase
// Load the preferences from an XML resource
@@ -124,7 +120,7 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment(), DatabaseRetriev
override fun onDatabaseRetrieved(database: Database?) {
mDatabase = database
mDatabaseReadOnly = mDatabaseReadOnly || database?.isReadOnly == true
mDatabaseReadOnly = database?.isReadOnly == true
mDatabase?.let {
if (it.loaded) {
@@ -674,18 +670,13 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment(), DatabaseRetriev
// Check the time lock before launching settings
// TODO activity menu
(activity as SettingsActivity?)?.let {
MenuUtil.onDefaultMenuOptionsItemSelected(it, item, mDatabaseReadOnly, true)
MenuUtil.onDefaultMenuOptionsItemSelected(it, item, true)
}
super.onOptionsItemSelected(item)
}
}
}
override fun onSaveInstanceState(outState: Bundle) {
ReadOnlyHelper.onSaveInstanceState(outState, mDatabaseReadOnly)
super.onSaveInstanceState(outState)
}
companion object {
private const val TAG_PREF_FRAGMENT = "TAG_PREF_FRAGMENT"
}

View File

@@ -26,7 +26,6 @@ import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.dialogs.UnderDevelopmentFeatureDialogFragment
import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper
abstract class NestedSettingsFragment : PreferenceFragmentCompat() {
@@ -62,7 +61,7 @@ abstract class NestedSettingsFragment : PreferenceFragmentCompat() {
private const val TAG_KEY = "NESTED_KEY"
fun newInstance(key: Screen, databaseReadOnly: Boolean = ReadOnlyHelper.READ_ONLY_DEFAULT)
fun newInstance(key: Screen)
: NestedSettingsFragment {
val fragment: NestedSettingsFragment = when (key) {
Screen.APPLICATION,
@@ -76,7 +75,6 @@ abstract class NestedSettingsFragment : PreferenceFragmentCompat() {
// supply arguments to bundle.
val args = Bundle()
args.putInt(TAG_KEY, key.ordinal)
ReadOnlyHelper.putReadOnlyInBundle(args, databaseReadOnly)
fragment.arguments = args
return fragment
}

View File

@@ -33,7 +33,6 @@ import androidx.fragment.app.Fragment
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.dialogs.AssignMasterKeyDialogFragment
import com.kunzisoft.keepass.activities.helpers.ExternalFileHelper
import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper
import com.kunzisoft.keepass.activities.legacy.LockingActivity
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.model.MainCredential
@@ -181,7 +180,7 @@ open class SettingsActivity
setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left,
R.anim.slide_in_left, R.anim.slide_out_right)
}
replace(R.id.fragment_container, NestedSettingsFragment.newInstance(key, mReadOnly), TAG_NESTED)
replace(R.id.fragment_container, NestedSettingsFragment.newInstance(key), TAG_NESTED)
addToBackStack(TAG_NESTED)
commit()
}
@@ -278,9 +277,8 @@ open class SettingsActivity
private const val TAG_NESTED = "TAG_NESTED"
private const val FRAGMENT_ARG = "FRAGMENT_ARG"
fun launch(activity: Activity, readOnly: Boolean, timeoutEnable: Boolean) {
fun launch(activity: Activity, timeoutEnable: Boolean) {
val intent = Intent(activity, SettingsActivity::class.java)
ReadOnlyHelper.putReadOnlyInIntent(intent, readOnly)
intent.putExtra(TIMEOUT_ENABLE_KEY, timeoutEnable)
if (!timeoutEnable) {
activity.startActivity(intent)

View File

@@ -28,7 +28,6 @@ import android.view.MenuItem
import com.kunzisoft.keepass.BuildConfig
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.AboutActivity
import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper.READ_ONLY_DEFAULT
import com.kunzisoft.keepass.settings.SettingsActivity
object MenuUtil {
@@ -52,7 +51,6 @@ object MenuUtil {
*/
fun onDefaultMenuOptionsItemSelected(activity: Activity,
item: MenuItem,
readOnly: Boolean = READ_ONLY_DEFAULT,
timeoutEnable: Boolean = false) {
when (item.itemId) {
R.id.menu_contribute -> {
@@ -60,7 +58,7 @@ object MenuUtil {
}
R.id.menu_app_settings -> {
// To avoid flickering when launch settings in a LockingActivity
SettingsActivity.launch(activity, readOnly, timeoutEnable)
SettingsActivity.launch(activity, timeoutEnable)
}
R.id.menu_about -> {
val intent = Intent(activity, AboutActivity::class.java)