Add option to always merge on conflict, closes https://github.com/PhilippC/keepass2android/issues/1218

This commit is contained in:
Philipp Crocoll
2022-01-15 15:55:34 +01:00
parent daecd42d8e
commit b4a82511ff
4 changed files with 40 additions and 13 deletions

View File

@@ -21,6 +21,7 @@ using System.Security.Cryptography;
using Android.App; using Android.App;
using Android.Content; using Android.Content;
using Android.OS; using Android.OS;
using Android.Preferences;
using Java.Lang; using Java.Lang;
using KeePassLib; using KeePassLib;
using KeePassLib.Serialization; using KeePassLib.Serialization;
@@ -37,7 +38,7 @@ namespace keepass2android
private readonly Database _db; private readonly Database _db;
private readonly bool _dontSave; private readonly bool _dontSave;
/// <summary> /// <summary>
/// stream for reading the data from the original file. If this is set to a non-null value, we know we need to sync /// stream for reading the data from the original file. If this is set to a non-null value, we know we need to sync
/// </summary> /// </summary>
private readonly Stream _streamForOrigFile; private readonly Stream _streamForOrigFile;
@@ -51,7 +52,7 @@ namespace keepass2android
_ctx = ctx; _ctx = ctx;
_app = app; _app = app;
_dontSave = dontSave; _dontSave = dontSave;
} }
/// <summary> /// <summary>
/// Constructor for sync /// Constructor for sync
@@ -129,6 +130,17 @@ namespace keepass2android
{ {
Kp2aLog.Log("Conflict. " + hasStreamForOrigFile + " " + hasChangeFast + " " + hasHashChanged); Kp2aLog.Log("Conflict. " + hasStreamForOrigFile + " " + hasChangeFast + " " + hasHashChanged);
bool alwaysMerge = (PreferenceManager.GetDefaultSharedPreferences(Application.Context)
.GetBoolean("AlwaysMergeOnConflict", false));
if (alwaysMerge)
{
MergeAndFinish(fileStorage, ioc);
}
else
{
//ask user... //ask user...
_app.AskYesNoCancel(UiStringKey.TitleSyncQuestion, UiStringKey.MessageSyncQuestion, _app.AskYesNoCancel(UiStringKey.TitleSyncQuestion, UiStringKey.MessageSyncQuestion,
UiStringKey.YesSynchronize, UiStringKey.YesSynchronize,
@@ -136,16 +148,7 @@ namespace keepass2android
//yes = sync //yes = sync
(sender, args) => (sender, args) =>
{ {
Action runHandler = () => Action runHandler = () => { MergeAndFinish(fileStorage, ioc); };
{
//note: when synced, the file might be downloaded once again from the server. Caching the data
//in the hashing function would solve this but increases complexity. I currently assume the files are
//small.
MergeIn(fileStorage, ioc);
PerformSaveWithoutCheck(fileStorage, ioc);
_db.UpdateGlobals();
Finish(true);
};
RunInWorkerThread(runHandler); RunInWorkerThread(runHandler);
}, },
//no = overwrite //no = overwrite
@@ -164,6 +167,8 @@ namespace keepass2android
}, },
_ctx _ctx
); );
}
} }
else else
{ {
@@ -193,7 +198,18 @@ namespace keepass2android
} }
private void RunInWorkerThread(Action runHandler) private void MergeAndFinish(IFileStorage fileStorage, IOConnectionInfo ioc)
{
//note: when synced, the file might be downloaded once again from the server. Caching the data
//in the hashing function would solve this but increases complexity. I currently assume the files are
//small.
MergeIn(fileStorage, ioc);
PerformSaveWithoutCheck(fileStorage, ioc);
_db.UpdateGlobals();
Finish(true);
}
private void RunInWorkerThread(Action runHandler)
{ {
try try
{ {

View File

@@ -50,6 +50,7 @@
<!-- Preference settings --> <!-- Preference settings -->
<string name="AutoReturnFromQuery_key">AutoReturnFromQuery_key</string> <string name="AutoReturnFromQuery_key">AutoReturnFromQuery_key</string>
<string name="AlwaysMergeOnConflict_key">AlwaysMergeOnConflict</string>
<string name="NoDalVerification_key">NoDalVerification_key</string> <string name="NoDalVerification_key">NoDalVerification_key</string>
<string name="InlineSuggestions_key">InlineSuggestions_key</string> <string name="InlineSuggestions_key">InlineSuggestions_key</string>
<string name="algorithm_key">algorithm</string> <string name="algorithm_key">algorithm</string>

View File

@@ -311,6 +311,8 @@
<string name="please_note">Please note</string> <string name="please_note">Please note</string>
<string name="contributors">Contributors</string> <string name="contributors">Contributors</string>
<string name="regular_expression">Regular expression</string> <string name="regular_expression">Regular expression</string>
<string name="AlwaysMergeOnConflict_title">Always merge on conflict</string>
<string name="AlwaysMergeOnConflict_summary">When Keepass2Android detects that the remote file was modified, always merge the local changes with the remote changes.</string>
<string name="TanExpiresOnUse_title">TAN expires on use</string> <string name="TanExpiresOnUse_title">TAN expires on use</string>
<string name="TanExpiresOnUse_summary">Mark TAN entries expired when using them</string> <string name="TanExpiresOnUse_summary">Mark TAN entries expired when using them</string>
<string name="ShowUsernameInList_title">Display username in list</string> <string name="ShowUsernameInList_title">Display username in list</string>

View File

@@ -576,6 +576,14 @@
android:key="@string/SyncAfterQuickUnlock_key" /> android:key="@string/SyncAfterQuickUnlock_key" />
<CheckBoxPreference <CheckBoxPreference
android:enabled="true"
android:persistent="true"
android:summary="@string/AlwaysMergeOnConflict_summary"
android:defaultValue="false"
android:title="@string/AlwaysMergeOnConflict_title"
android:key="@string/AlwaysMergeOnConflict_key" />
<CheckBoxPreference
android:key="@string/TanExpiresOnUse_key" android:key="@string/TanExpiresOnUse_key"
android:title="@string/TanExpiresOnUse_title" android:title="@string/TanExpiresOnUse_title"
android:summary="@string/TanExpiresOnUse_summary" android:summary="@string/TanExpiresOnUse_summary"