rename RunnableOnFinish => OperationWithFinishHandler, introduce IKp2aStatusLogger

This commit is contained in:
Philipp Crocoll
2025-04-29 13:48:24 +02:00
parent 61c871f782
commit c0ed185612
28 changed files with 186 additions and 111 deletions

View File

@@ -86,6 +86,11 @@ namespace KeePassLib.Interfaces
/// the current work.</returns>
bool SetText(string strNewText, LogStatusType lsType);
void UpdateMessage(String message);
void UpdateSubMessage(String submessage);
/// <summary>
/// Check if the user cancelled the current work.
/// </summary>
@@ -100,6 +105,12 @@ namespace KeePassLib.Interfaces
public void EndLogging() { }
public bool SetProgress(uint uPercent) { return true; }
public bool SetText(string strNewText, LogStatusType lsType) { return true; }
public void UpdateMessage(string message){
}
public void UpdateSubMessage(string submessage){
}
public bool ContinueWork() { return true; }
}
}

View File

@@ -52,7 +52,7 @@ namespace keepass2android
/// <summary>
/// Loads the specified data as the currently open database, as unlocked.
/// </summary>
Database LoadDatabase(IOConnectionInfo ioConnectionInfo, MemoryStream memoryStream, CompositeKey compKey, ProgressDialogStatusLogger statusLogger, IDatabaseFormat databaseFormat, bool makeCurrent);
Database LoadDatabase(IOConnectionInfo ioConnectionInfo, MemoryStream memoryStream, CompositeKey compKey, IKp2aStatusLogger statusLogger, IDatabaseFormat databaseFormat, bool makeCurrent);
HashSet<PwGroup> DirtyGroups { get; }

View File

@@ -22,12 +22,132 @@ using KeePassLib.Interfaces;
namespace keepass2android
{
public interface IKp2aStatusLogger : IStatusLogger
{
void UpdateMessage(UiStringKey stringKey);
}
public class Kp2aNullStatusLogger : IKp2aStatusLogger
{
public void StartLogging(string strOperation, bool bWriteOperationToLog)
{
}
public void EndLogging()
{
}
public bool SetProgress(uint uPercent)
{
return true;
}
public bool SetText(string strNewText, LogStatusType lsType)
{
return true;
}
public void UpdateMessage(string message)
{
}
public void UpdateSubMessage(string submessage)
{
}
public bool ContinueWork()
{
return true;
}
public void UpdateMessage(UiStringKey stringKey)
{
}
}
public abstract class Kp2aAppStatusLogger : IKp2aStatusLogger
{
protected IKp2aApp _app;
public Kp2aAppStatusLogger(IKp2aApp app)
{
_app = app;
}
#region IStatusLogger implementation
public void StartLogging(string strOperation, bool bWriteOperationToLog)
{
}
public void EndLogging()
{
}
public bool SetProgress(uint uPercent)
{
return true;
}
public bool SetText(string strNewText, LogStatusType lsType)
{
if (strNewText.StartsWith("KP2AKEY_"))
{
UiStringKey key;
if (Enum.TryParse(strNewText.Substring("KP2AKEY_".Length), true, out key))
{
UpdateMessage(_app.GetResourceString(key), lsType);
return true;
}
}
UpdateMessage(strNewText, lsType);
return true;
}
public abstract void UpdateMessage(string message);
public abstract void UpdateSubMessage(string submessage);
private void UpdateMessage(string message, LogStatusType lsType)
{
if (lsType == LogStatusType.AdditionalInfo)
{
UpdateSubMessage(message);
}
else
{
UpdateMessage(message);
}
}
public bool ContinueWork()
{
return true;
}
#endregion
public void UpdateMessage(UiStringKey stringKey)
{
if (_app != null)
UpdateMessage(_app.GetResourceString(stringKey));
}
}
/// <summary>
/// StatusLogger implementation which shows the progress in a progress dialog
/// </summary>
public class ProgressDialogStatusLogger: IStatusLogger {
public class ProgressDialogStatusLogger: Kp2aAppStatusLogger
{
private readonly IProgressDialog _progressDialog;
readonly IKp2aApp _app;
private readonly Handler _handler;
private string _message = "";
private string _submessage;
@@ -35,22 +155,15 @@ namespace keepass2android
public String SubMessage => _submessage;
public String Message => _message;
public ProgressDialogStatusLogger() {
}
public ProgressDialogStatusLogger(IKp2aApp app, Handler handler, IProgressDialog pd) {
_app = app;
public ProgressDialogStatusLogger(IKp2aApp app, Handler handler, IProgressDialog pd)
: base(app){
_progressDialog = pd;
_handler = handler;
}
public void UpdateMessage(UiStringKey stringKey) {
if (_app != null)
UpdateMessage(_app.GetResourceString(stringKey));
}
public void UpdateMessage (String message)
public override void UpdateMessage (String message)
{
Kp2aLog.Log("status message: " + message);
_message = message;
@@ -59,7 +172,7 @@ namespace keepass2android
}
}
public void UpdateSubMessage(String submessage)
public override void UpdateSubMessage(String submessage)
{
Kp2aLog.Log("status submessage: " + submessage);
_submessage = submessage;
@@ -80,57 +193,6 @@ namespace keepass2android
}
}
#region IStatusLogger implementation
public void StartLogging (string strOperation, bool bWriteOperationToLog)
{
}
public void EndLogging ()
{
}
public bool SetProgress (uint uPercent)
{
return true;
}
public bool SetText (string strNewText, LogStatusType lsType)
{
if (strNewText.StartsWith("KP2AKEY_"))
{
UiStringKey key;
if (Enum.TryParse(strNewText.Substring("KP2AKEY_".Length), true, out key))
{
UpdateMessage(_app.GetResourceString(key), lsType);
return true;
}
}
UpdateMessage(strNewText, lsType);
return true;
}
private void UpdateMessage(string message, LogStatusType lsType)
{
if (lsType == LogStatusType.AdditionalInfo)
{
UpdateSubMessage(message);
}
else
{
UpdateMessage(message);
}
}
public bool ContinueWork ()
{
return true;
}
#endregion
}
}

View File

@@ -72,14 +72,14 @@ namespace keepass2android
}
private readonly Handler _handler;
private readonly RunnableOnFinish _task;
private readonly OperationWithFinishHandler _task;
private IProgressDialog _progressDialog;
private readonly IKp2aApp _app;
private Java.Lang.Thread _thread;
private Activity _activeActivity, _previouslyActiveActivity;
private ProgressDialogStatusLogger _progressDialogStatusLogger;
public ProgressTask(IKp2aApp app, Activity activity, RunnableOnFinish task)
public ProgressTask(IKp2aApp app, Activity activity, OperationWithFinishHandler task)
{
_activeActivity = activity;
_task = task;

View File

@@ -13,7 +13,7 @@ using keepass2android.Io;
namespace keepass2android
{
public class CheckDatabaseForChanges: RunnableOnFinish
public class CheckDatabaseForChanges: OperationWithFinishHandler
{
private readonly Context _context;
private readonly IKp2aApp _app;

View File

@@ -85,7 +85,7 @@ namespace keepass2android
/// <summary>
/// Do not call this method directly. Call App.Kp2a.LoadDatabase instead.
/// </summary>
public void LoadData(IKp2aApp app, IOConnectionInfo iocInfo, MemoryStream databaseData, CompositeKey compositeKey, ProgressDialogStatusLogger status, IDatabaseFormat databaseFormat)
public void LoadData(IKp2aApp app, IOConnectionInfo iocInfo, MemoryStream databaseData, CompositeKey compositeKey, IKp2aStatusLogger status, IDatabaseFormat databaseFormat)
{
PwDatabase pwDatabase = new PwDatabase();
@@ -149,7 +149,7 @@ namespace keepass2android
get { return GetFingerprintModePrefKey(Ioc); }
}
protected virtual void PopulateDatabaseFromStream(PwDatabase pwDatabase, Stream s, IOConnectionInfo iocInfo, CompositeKey compositeKey, ProgressDialogStatusLogger status, IDatabaseFormat databaseFormat)
protected virtual void PopulateDatabaseFromStream(PwDatabase pwDatabase, Stream s, IOConnectionInfo iocInfo, CompositeKey compositeKey, IKp2aStatusLogger status, IDatabaseFormat databaseFormat)
{
IFileStorage fileStorage = _app.GetFileStorage(iocInfo);
var filename = fileStorage.GetFilenameWithoutPathAndExt(iocInfo);

View File

@@ -10,7 +10,7 @@ using KeePass.Util;
namespace keepass2android
{
public class SynchronizeCachedDatabase: RunnableOnFinish
public class SynchronizeCachedDatabase: OperationWithFinishHandler
{
private readonly Activity _context;
private readonly IKp2aApp _app;

View File

@@ -21,7 +21,7 @@ using KeePassLib;
namespace keepass2android
{
public class AddEntry : RunnableOnFinish {
public class AddEntry : OperationWithFinishHandler {
protected Database Db
{
get { return _app.CurrentDb; }

View File

@@ -23,7 +23,7 @@ using KeePassLib;
namespace keepass2android
{
public class AddGroup : RunnableOnFinish {
public class AddGroup : OperationWithFinishHandler {
internal Database Db
{
get { return _app.CurrentDb; }

View File

@@ -26,7 +26,7 @@ using KeePassLib.Utility;
namespace keepass2android
{
public class AddTemplateEntries : RunnableOnFinish {
public class AddTemplateEntries : OperationWithFinishHandler {
public class TemplateEntry
{

View File

@@ -26,7 +26,7 @@ using KeePassLib.Keys;
namespace keepass2android
{
public class CreateDb : RunnableOnFinish {
public class CreateDb : OperationWithFinishHandler {
private readonly IOConnectionInfo _ioc;
private readonly bool _dontSave;
private readonly Activity _ctx;

View File

@@ -6,7 +6,7 @@ using KeePassLib;
namespace keepass2android
{
public abstract class DeleteRunnable : RunnableOnFinish
public abstract class DeleteRunnable : OperationWithFinishHandler
{
protected DeleteRunnable(Activity activity, OnOperationFinishedHandler operationFinishedHandler, IKp2aApp app)
: base(activity, operationFinishedHandler)

View File

@@ -23,7 +23,7 @@ using KeePassLib;
namespace keepass2android
{
public class EditGroup : RunnableOnFinish {
public class EditGroup : OperationWithFinishHandler {
internal Database Db
{
get { return _app.FindDatabaseForElement(Group); }

View File

@@ -29,7 +29,7 @@ using KeePassLib.Serialization;
namespace keepass2android
{
public class LoadDb : RunnableOnFinish {
public class LoadDb : OperationWithFinishHandler {
private readonly IOConnectionInfo _ioc;
private readonly Task<MemoryStream> _databaseData;
private readonly CompositeKey _compositeKey;

View File

@@ -10,7 +10,7 @@ using KeePassLib.Interfaces;
namespace keepass2android.database.edit
{
public class MoveElements: RunnableOnFinish
public class MoveElements: OperationWithFinishHandler
{
private readonly List<IStructureItem> _elementsToMove;
private readonly PwGroup _targetGroup;

View File

@@ -22,6 +22,7 @@ using Android.Content;
using Android.OS;
using Android.Widget;
using Google.Android.Material.Dialog;
using KeePassLib.Interfaces;
namespace keepass2android
{
@@ -39,11 +40,11 @@ namespace keepass2android
protected OnOperationFinishedHandler NextOnOperationFinishedHandler;
protected Handler Handler;
private ProgressDialogStatusLogger _statusLogger = new ProgressDialogStatusLogger(); //default: no logging but not null -> can be used whenever desired
private IKp2aStatusLogger _statusLogger = new Kp2aNullStatusLogger(); //default: no logging but not null -> can be used whenever desired
private Activity _activeActivity, _previouslyActiveActivity;
public ProgressDialogStatusLogger StatusLogger
public IKp2aStatusLogger StatusLogger
{
get { return _statusLogger; }
set { _statusLogger = value; }

View File

@@ -17,17 +17,18 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
using System;
using Android.App;
using Android.Content;
using KeePassLib.Interfaces;
namespace keepass2android
{
public abstract class RunnableOnFinish {
public abstract class OperationWithFinishHandler {
protected OnOperationFinishedHandler _operationFinishedHandler;
public ProgressDialogStatusLogger StatusLogger = new ProgressDialogStatusLogger(); //default: empty but not null
public IKp2aStatusLogger StatusLogger = new Kp2aNullStatusLogger(); //default: empty but not null
private Activity _activeActivity;
protected RunnableOnFinish(Activity activeActivity, OnOperationFinishedHandler operationFinishedHandler)
protected OperationWithFinishHandler(Activity activeActivity, OnOperationFinishedHandler operationFinishedHandler)
{
_activeActivity = activeActivity;
_operationFinishedHandler = operationFinishedHandler;
@@ -64,12 +65,12 @@ namespace keepass2android
}
}
public void SetStatusLogger(ProgressDialogStatusLogger status) {
public void SetStatusLogger(IKp2aStatusLogger statusLogger) {
if (operationFinishedHandler != null)
{
operationFinishedHandler.StatusLogger = status;
operationFinishedHandler.StatusLogger = statusLogger;
}
StatusLogger = status;
StatusLogger = statusLogger;
}
public abstract void Run();

View File

@@ -34,7 +34,7 @@ using KeePass.Util;
namespace keepass2android
{
public class SaveDb : RunnableOnFinish {
public class SaveDb : OperationWithFinishHandler {
private readonly IKp2aApp _app;
private readonly Database _db;
private readonly bool _dontSave;

View File

@@ -22,7 +22,7 @@ using KeePassLib.Keys;
namespace keepass2android
{
public class SetPassword : RunnableOnFinish {
public class SetPassword : OperationWithFinishHandler {
private readonly String _password;
private readonly String _keyfile;

View File

@@ -22,7 +22,7 @@ using KeePassLib;
namespace keepass2android
{
public class UpdateEntry : RunnableOnFinish {
public class UpdateEntry : OperationWithFinishHandler {
private readonly IKp2aApp _app;
private readonly Activity _ctx;

View File

@@ -4,7 +4,7 @@ using KeePassLib.Serialization;
namespace keepass2android
{
class CreateNewFilename : RunnableOnFinish
class CreateNewFilename : OperationWithFinishHandler
{
private readonly string _filename;

View File

@@ -1260,7 +1260,7 @@ namespace keepass2android
}
public class WriteBinaryTask : RunnableOnFinish
public class WriteBinaryTask : OperationWithFinishHandler
{
private readonly IKp2aApp _app;
private readonly ProtectedBinary _data;
@@ -1511,7 +1511,7 @@ namespace keepass2android
});
RunnableOnFinish runnable = new UpdateEntry(this, App.Kp2a, initialEntry, newEntry, closeOrShowError);
OperationWithFinishHandler runnable = new UpdateEntry(this, App.Kp2a, initialEntry, newEntry, closeOrShowError);
ProgressTask pt = new ProgressTask(App.Kp2a, this, runnable);
pt.Run();

View File

@@ -519,7 +519,7 @@ namespace keepass2android
SetResult(KeePass.ExitRefreshTitle);
//}
RunnableOnFinish runnable;
OperationWithFinishHandler runnable;
ActionOnOperationFinished closeOrShowError = new ActionOnOperationFinished(this, (success, message, activity) => {
if (success)

View File

@@ -93,7 +93,7 @@ namespace keepass2android
get { return 0; }
}
public class ExportDb : RunnableOnFinish
public class ExportDb : OperationWithFinishHandler
{
private readonly IKp2aApp _app;
private readonly FileFormatProvider _fileFormat;

View File

@@ -202,7 +202,7 @@ namespace keepass2android
String strGroupUuid = data.Extras.GetString(GroupEditActivity.KeyGroupUuid);
GroupBaseActivity act = this;
Handler handler = new Handler();
RunnableOnFinish task;
OperationWithFinishHandler task;
if (strGroupUuid == null)
{
task = AddGroup.GetInstance(this, App.Kp2a, groupName, groupIconId, groupCustomIconId, Group, new RefreshTask(handler, this), false);

View File

@@ -15,7 +15,7 @@ namespace keepass2android
_activity = activity;
}
public class SyncOtpAuxFile : RunnableOnFinish
public class SyncOtpAuxFile : OperationWithFinishHandler
{
private readonly IOConnectionInfo _ioc;
@@ -52,7 +52,7 @@ namespace keepass2android
public void SynchronizeDatabase(Action runAfterSuccess)
{
var filestorage = App.Kp2a.GetFileStorage(App.Kp2a.CurrentDb.Ioc);
RunnableOnFinish task;
OperationWithFinishHandler task;
OnOperationFinishedHandler onOperationFinishedHandler = new ActionOnOperationFinished(_activity, (success, message, activity) =>
{
if (!String.IsNullOrEmpty(message))

View File

@@ -186,7 +186,7 @@ namespace keepass2android
public Database LoadDatabase(IOConnectionInfo ioConnectionInfo, MemoryStream memoryStream, CompositeKey compositeKey, ProgressDialogStatusLogger statusLogger, IDatabaseFormat databaseFormat, bool makeCurrent)
public Database LoadDatabase(IOConnectionInfo ioConnectionInfo, MemoryStream memoryStream, CompositeKey compositeKey, IKp2aStatusLogger statusLogger, IDatabaseFormat databaseFormat, bool makeCurrent)
{
var prefs = PreferenceManager.GetDefaultSharedPreferences(LocaleManager.LocalizedAppContext);
var createBackup = prefs.GetBoolean(LocaleManager.LocalizedAppContext.GetString(Resource.String.CreateBackups_key), true)

View File

@@ -13,7 +13,7 @@ namespace keepass2android
public class ExportKeyfileActivity : LockCloseActivity
{
public class ExportKeyfile : RunnableOnFinish
public class ExportKeyfile : OperationWithFinishHandler
{
private readonly IKp2aApp _app;
private IOConnectionInfo _targetIoc;