Compare commits

...

28 Commits

Author SHA1 Message Date
Philipp Crocoll
c3c9c8610d remove debug logging 2025-06-03 16:00:24 +02:00
Philipp Crocoll
9c2d50c7b0 remove debugging code, resolve some TODOs 2025-06-03 15:54:34 +02:00
Philipp Crocoll
0cd9df7415 fix issues with background sync and multiple databases (especially autoopen) 2025-06-03 15:37:21 +02:00
Philipp Crocoll
a4a3112dc6 fix issues with background operations implementation, added more logging 2025-06-03 14:44:01 +02:00
Philipp Crocoll
6351e1f3d0 towards unifying Background and Blocking Operation Runners. Not working yet, does it run more in non-ui thread? 2025-06-03 11:12:37 +02:00
Philipp Crocoll
2959d8cbcc improve intent when clicking the background-operation notification 2025-06-03 10:16:48 +02:00
Philipp Crocoll
c98680347a fix implementation for BackgroundSyncService for blocking operations 2025-05-27 16:29:42 +02:00
Philipp Crocoll
35367bb28b use ActionInContextInstanceOnOperationFinished where appropriate 2025-05-27 15:54:01 +02:00
Philipp Crocoll
8a1890bc10 refactor and improve EntryEditActivity: Ensure that configuration changes are handled properly 2025-05-27 15:26:09 +02:00
Philipp Crocoll
bf4035fcfe refactor EntryEditActivity, extracting views for entry-edit sections 2025-05-27 12:36:37 +02:00
Philipp Crocoll
d3dfbaab4b start using Pending Actions in EntryEditActivity. Extra fields still causing trouble on config change. 2025-05-27 11:59:26 +02:00
Philipp Crocoll
227074efb6 remove superfluous character from layout 2025-05-27 11:58:47 +02:00
Philipp Crocoll
69f79c1b20 change blocking operations to use BackgroundSyncService also. Introduce pending actions for context instances. 2025-05-27 11:57:58 +02:00
Philipp Crocoll
000d1254ec make parameter naming more consistent 2025-05-27 11:54:55 +02:00
Philipp Crocoll
324fc1f2ee allow to cancel background operations manually or when another operation starts; block database updates in certain activities, e.g. EntryEdit; 2025-05-20 17:05:13 +02:00
Philipp Crocoll
52121c6a85 split file into one file per class 2025-05-13 21:46:24 +02:00
Philipp Crocoll
c6f494ac33 remove no longer needed lock 2025-05-13 21:37:45 +02:00
Philipp Crocoll
400e171bc5 implement UI updates after background sync for Group activity and Entry activity 2025-05-13 21:34:06 +02:00
Philipp Crocoll
41e6e67e87 add support for yes/no/cancel question during background sync 2025-05-13 16:42:50 +02:00
Philipp Crocoll
8277283ebc only enable sync-in-background mode when file is already cached 2025-05-13 14:52:13 +02:00
Philipp Crocoll
71806178d0 tasks no longer store references to activities. These can "expire" and are hard to update (e.g. if one task creates another). Instead, the app can provide the currently active context. 2025-05-13 14:51:44 +02:00
Philipp Crocoll
aa2e4b856d introduce BackgroundSyncService to keep app alive while sync is going on. 2025-05-13 12:38:21 +02:00
Philipp Crocoll
cfb185b53d LoadDb and SaveDb respect the SyncInBackground preference 2025-05-13 11:10:26 +02:00
Philipp Crocoll
c3b6612591 let database sync run in the background. not handling all cases yet. 2025-05-13 09:23:57 +02:00
Philipp Crocoll
fefcf8f30e rename SynchronizeDatabase => StartSynchronizeDatabase; add experimental UI for displaying sync-in-progress 2025-05-06 13:29:47 +02:00
Philipp Crocoll
e95cc84a15 rename ProgressTask => BlockingOperationRunner 2025-04-29 15:10:54 +02:00
Philipp Crocoll
c0ed185612 rename RunnableOnFinish => OperationWithFinishHandler, introduce IKp2aStatusLogger 2025-04-29 15:08:09 +02:00
Philipp Crocoll
61c871f782 rename OnFinish => OnOperationFinishedHandler 2025-04-29 13:45:24 +02:00
84 changed files with 3336 additions and 1759 deletions

View File

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

View File

@@ -21,6 +21,7 @@ using System.IO;
using Android; using Android;
using Android.App; using Android.App;
using Android.Content; using Android.Content;
using Android.OS;
using Android.Preferences; using Android.Preferences;
using KeePassLib.Serialization; using KeePassLib.Serialization;
@@ -35,7 +36,15 @@ namespace keepass2android
public static void Log(string message) public static void Log(string message)
{ {
if (message != null) if (message != null)
{
message += Thread.CurrentThread.ManagedThreadId != 0
? " (Thread ID: " + Thread.CurrentThread.ManagedThreadId + ")"
: "";
if (Looper.MainLooper == Looper.MyLooper())
message += " (Main Looper)";
Android.Util.Log.Debug("KP2A", message); Android.Util.Log.Debug("KP2A", message);
}
if (LogToFile) if (LogToFile)
{ {
lock (_fileLocker) lock (_fileLocker)

View File

@@ -208,7 +208,7 @@ namespace KeePassLib.Serialization
if (!string.IsNullOrEmpty(strHash) && (m_pbHashOfHeader != null) && if (!string.IsNullOrEmpty(strHash) && (m_pbHashOfHeader != null) &&
!m_bRepairMode) !m_bRepairMode)
{ {
Debug.Assert(m_uFileVersion < FileVersion32_4); // Debug.Assert(m_uFileVersion < FileVersion32_4);
byte[] pbHash = Convert.FromBase64String(strHash); byte[] pbHash = Convert.FromBase64String(strHash);
if (!MemUtil.ArraysEqual(pbHash, m_pbHashOfHeader)) if (!MemUtil.ArraysEqual(pbHash, m_pbHashOfHeader))

View File

@@ -488,7 +488,7 @@ namespace KeePassLib.Serialization
ProtectedBinary pb = new ProtectedBinary(bProt, pbData, ProtectedBinary pb = new ProtectedBinary(bProt, pbData,
1, pbData.Length - 1); 1, pbData.Length - 1);
Debug.Assert(m_pbsBinaries.Find(pb) < 0); // No deduplication? //Debug.Assert(m_pbsBinaries.Find(pb) < 0); // No deduplication?
m_pbsBinaries.Add(pb); m_pbsBinaries.Add(pb);
if (bProt) MemUtil.ZeroByteArray(pbData); if (bProt) MemUtil.ZeroByteArray(pbData);

View File

@@ -0,0 +1,53 @@
/*
This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file is based on Keepassdroid, Copyright Brian Pellin.
Keepass2Android 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 2 of the License, or
(at your option) any later version.
Keepass2Android 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 Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/
using Android.App;
using Android.Content;
using Android.OS;
using Java.Lang;
using Java.Security;
using System.Threading.Tasks;
namespace keepass2android
{
/// <summary>
/// Class to run a task while a progress dialog is shown
/// </summary>
public class BlockingOperationStarter
{
private readonly OperationWithFinishHandler _task;
private readonly IKp2aApp _app;
public BlockingOperationStarter(IKp2aApp app, OperationWithFinishHandler task)
{
_task = task;
_app = app;
}
public void Run()
{
_app.CancelBackgroundOperations();
OperationRunner.Instance.Run(_app, _task, true);
}
}
}

View File

@@ -41,7 +41,7 @@ namespace keepass2android
/// Interface through which Activities and the logic layer can access some app specific functionalities and Application static data /// Interface through which Activities and the logic layer can access some app specific functionalities and Application static data
/// </summary> /// </summary>
/// This also contains methods which are UI specific and should be replacable for testing. /// This also contains methods which are UI specific and should be replacable for testing.
public interface IKp2aApp : ICertificateValidationHandler public interface IKp2aApp : ICertificateValidationHandler, IActiveContextProvider
{ {
/// <summary> /// <summary>
/// Locks all currently open databases, quicklocking if available (unless false is passed for allowQuickUnlock) /// Locks all currently open databases, quicklocking if available (unless false is passed for allowQuickUnlock)
@@ -52,7 +52,9 @@ namespace keepass2android
/// <summary> /// <summary>
/// Loads the specified data as the currently open database, as unlocked. /// Loads the specified data as the currently open database, as unlocked.
/// </summary> /// </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,
IDatabaseModificationWatcher modificationWatcher);
HashSet<PwGroup> DirtyGroups { get; } HashSet<PwGroup> DirtyGroups { get; }
@@ -96,7 +98,6 @@ namespace keepass2android
EventHandler<DialogClickEventArgs> yesHandler, EventHandler<DialogClickEventArgs> yesHandler,
EventHandler<DialogClickEventArgs> noHandler, EventHandler<DialogClickEventArgs> noHandler,
EventHandler<DialogClickEventArgs> cancelHandler, EventHandler<DialogClickEventArgs> cancelHandler,
Context ctx,
string messageSuffix = ""); string messageSuffix = "");
/// <summary> /// <summary>
@@ -107,7 +108,6 @@ namespace keepass2android
EventHandler<DialogClickEventArgs> yesHandler, EventHandler<DialogClickEventArgs> yesHandler,
EventHandler<DialogClickEventArgs> noHandler, EventHandler<DialogClickEventArgs> noHandler,
EventHandler<DialogClickEventArgs> cancelHandler, EventHandler<DialogClickEventArgs> cancelHandler,
Context ctx,
string messageSuffix = ""); string messageSuffix = "");
void ShowMessage(Context ctx, int resourceId, MessageSeverity severity); void ShowMessage(Context ctx, int resourceId, MessageSeverity severity);
@@ -136,10 +136,17 @@ namespace keepass2android
bool CheckForDuplicateUuids { get; } bool CheckForDuplicateUuids { get; }
#if !NoNet && !EXCLUDE_JAVAFILESTORAGE #if !NoNet && !EXCLUDE_JAVAFILESTORAGE
ICertificateErrorHandler CertificateErrorHandler { get; } ICertificateErrorHandler CertificateErrorHandler { get; }
#endif #endif
bool SyncInBackgroundPreference { get; set; }
void StartBackgroundSyncService();
ReaderWriterLockSlim DatabasesBackgroundModificationLock { get; }
bool CancelBackgroundOperations();
/// <summary>
/// Registers an action that should be executed when the context instance with the given id has been resumed.
/// </summary>
void RegisterPendingActionForContextInstance(int contextInstanceId, ActionOnOperationFinished actionToPerformWhenContextIsResumed);
} }
} }

View File

@@ -186,8 +186,11 @@ namespace keepass2android.Io
Kp2aLog.Log("couldn't open from remote " + ioc.Path); Kp2aLog.Log("couldn't open from remote " + ioc.Path);
#endif #endif
Kp2aLog.Log(ex.ToString()); Kp2aLog.Log(ex.ToString());
if (TriggerWarningWhenFallingBackToCache)
{
_cacheSupervisor.CouldntOpenFromRemote(ioc, ex); _cacheSupervisor.CouldntOpenFromRemote(ioc, ex);
}
return File.OpenRead(cachedFilePath); return File.OpenRead(cachedFilePath);
} }
} }
@@ -327,7 +330,10 @@ namespace keepass2android.Io
Kp2aLog.Log("couldn't save to remote " + ioc.Path); Kp2aLog.Log("couldn't save to remote " + ioc.Path);
Kp2aLog.Log(e.ToString()); Kp2aLog.Log(e.ToString());
//notify the supervisor so it might display a warning or schedule a retry //notify the supervisor so it might display a warning or schedule a retry
_cacheSupervisor.CouldntSaveToRemote(ioc, e); if (TriggerWarningWhenFallingBackToCache)
{
_cacheSupervisor.CouldntSaveToRemote(ioc, e); }
return false; return false;
} }
} }
@@ -632,6 +638,8 @@ namespace keepass2android.Io
set { _cachedStorage.IsOffline = value; } set { _cachedStorage.IsOffline = value; }
} }
public bool TriggerWarningWhenFallingBackToCache { get; set; }
public void OnRequestPermissionsResult(IFileStorageSetupActivity fileStorageSetupActivity, int requestCode, public void OnRequestPermissionsResult(IFileStorageSetupActivity fileStorageSetupActivity, int requestCode,
string[] permissions, Permission[] grantResults) string[] permissions, Permission[] grantResults)
{ {

View File

@@ -111,6 +111,11 @@ namespace keepass2android.Io
} }
Java.Lang.Exception exception = e as Java.Lang.Exception; Java.Lang.Exception exception = e as Java.Lang.Exception;
if ((exception is Java.Lang.InterruptedException) || (exception is Java.IO.InterruptedIOException))
{
throw new Java.Lang.InterruptedException(exception.Message);
}
if (exception != null) if (exception != null)
{ {
var ex = new Exception(exception.LocalizedMessage ?? var ex = new Exception(exception.LocalizedMessage ??

View File

@@ -11,6 +11,7 @@ namespace keepass2android.Io
public interface IOfflineSwitchable public interface IOfflineSwitchable
{ {
bool IsOffline { get; set; } bool IsOffline { get; set; }
bool TriggerWarningWhenFallingBackToCache { get; set; }
} }
/// <summary> /// <summary>
@@ -21,6 +22,7 @@ namespace keepass2android.Io
{ {
private readonly IFileStorage _baseStorage; private readonly IFileStorage _baseStorage;
public bool IsOffline { get; set; } public bool IsOffline { get; set; }
public bool TriggerWarningWhenFallingBackToCache { get; set; }
public OfflineSwitchableFileStorage(IFileStorage baseStorage) public OfflineSwitchableFileStorage(IFileStorage baseStorage)
{ {

View File

@@ -0,0 +1,239 @@
using Android.App;
using Android.Content;
using Android.OS;
using System.Threading.Tasks;
using Thread = Java.Lang.Thread;
namespace keepass2android;
/// <summary>
/// Allows to run tasks in the background. The UI is not blocked by the task. Tasks continue to run in the BackgroundSyncService if the app goes to background while tasks are active.
/// </summary>
public class OperationRunner
{
//singleton instance
private static OperationRunner _instance = null;
public static OperationRunner Instance
{
get
{
if (_instance == null)
{
_instance = new OperationRunner();
}
return _instance;
}
}
void Initialize(IKp2aApp app)
{
}
public struct OperationWithMetadata
{
public OperationWithMetadata()
{
Operation = null;
}
public OperationWithFinishHandler Operation { get; set; }
public bool RunBlocking { get; set; } = false;
}
public ProgressUiAsStatusLoggerAdapter StatusLogger => _statusLogger;
private OperationRunner()
{
//private constructor
}
private readonly Queue<OperationWithMetadata> _taskQueue = new Queue<OperationWithMetadata>();
private readonly object _taskQueueLock = new object();
private Java.Lang.Thread? _thread = null;
private OperationWithMetadata? _currentlyRunningTask = null;
private ProgressUiAsStatusLoggerAdapter _statusLogger = null;
private IProgressDialog _progressDialog;
private IKp2aApp _app;
public void Run(IKp2aApp app, OperationWithFinishHandler operation, bool runBlocking = false)
{
lock (Instance._taskQueueLock)
{
_taskQueue.Enqueue(new OperationWithMetadata(){ Operation = operation, RunBlocking = runBlocking});
operation.SetStatusLogger(_statusLogger);
// Start thread to run the task (unless it's already running)
if (_thread == null)
{
_statusLogger.StartLogging("", false);
_thread = new Java.Lang.Thread(() =>
{
while (true)
{
lock (_taskQueueLock)
{
if (!_taskQueue.Any())
{
_thread = null;
_statusLogger.EndLogging();
break;
}
else
{
_currentlyRunningTask = _taskQueue.Dequeue();
}
}
if (_currentlyRunningTask.Value.RunBlocking)
{
app.UiThreadHandler.Post(
() =>
{
TrySetupProgressDialog();
});
}
var originalFinishedHandler = _currentlyRunningTask.Value.Operation.operationFinishedHandler;
_currentlyRunningTask.Value.Operation.operationFinishedHandler = new ActionOnOperationFinished(app, (
(success, message, context) =>
{
if (_currentlyRunningTask?.RunBlocking == true)
{
_app.UiThreadHandler.Post(() =>
{
_progressDialog?.Dismiss();
}
);
}
_currentlyRunningTask = null;
}), originalFinishedHandler);
_currentlyRunningTask.Value.Operation.Run();
while (_currentlyRunningTask != null)
{
try
{
Thread.Sleep(100);
}
catch (Exception e)
{
Kp2aLog.Log("Thread interrupted.");
}
}
}
});
_thread.Start();
}
}
}
private bool TrySetupProgressDialog()
{
string currentMessage = "Initializing...";
string currentSubmessage = "";
if (_statusLogger != null)
{
currentMessage = _statusLogger.LastMessage;
currentSubmessage = _statusLogger.LastSubMessage;
}
if (_progressDialog != null)
{
var pd = _progressDialog;
_app.UiThreadHandler.Post(() =>
{
pd.Dismiss();
});
}
// Show process dialog
_progressDialog = _app.CreateProgressDialog(_app.ActiveContext);
if (_progressDialog == null)
{
return false;
}
var progressUi = new ProgressDialogUi(_app, _app.UiThreadHandler, _progressDialog);
_statusLogger.SetNewProgressUi(progressUi);
_statusLogger.StartLogging("", false);
_statusLogger.UpdateMessage(currentMessage);
_statusLogger.UpdateSubMessage(currentSubmessage);
return true;
}
public void SetNewActiveContext(IKp2aApp app)
{
_app = app;
Context? context = app.ActiveContext;
bool isAppContext = context == null || (context.ApplicationContext == context);
lock (_taskQueueLock)
{
if (isAppContext && _thread != null)
{
//this will register the service as new active context (see BackgroundSyncService.OnStartCommand())
app.StartBackgroundSyncService();
return;
}
if (_currentlyRunningTask?.RunBlocking == true && (context is Activity { IsFinishing: false, IsDestroyed:false}))
{
app.UiThreadHandler.Post(() =>
{
TrySetupProgressDialog();
});
}
else
{
var progressUi = (context as IProgressUiProvider)?.ProgressUi;
if (_statusLogger == null)
{
_statusLogger = new ProgressUiAsStatusLoggerAdapter(progressUi, app);
}
else
{
_statusLogger.SetNewProgressUi(progressUi);
}
}
foreach (var task in _taskQueue.Concat(_currentlyRunningTask == null ?
new List<OperationWithMetadata>() : [_currentlyRunningTask.Value])
)
{
task.Operation.SetStatusLogger(_statusLogger);
}
}
}
public void CancelAll()
{
lock (_taskQueueLock)
{
if (_thread != null)
{
_thread.Interrupt();
_thread = null;
_statusLogger?.EndLogging();
}
_taskQueue.Clear();
_currentlyRunningTask = null;
}
}
}

View File

@@ -22,43 +22,97 @@ using KeePassLib.Interfaces;
namespace keepass2android namespace keepass2android
{ {
public interface IKp2aStatusLogger : IStatusLogger
{
void UpdateMessage(UiStringKey stringKey);
string LastMessage { get; }
string LastSubMessage { get; }
}
public interface IProgressUi
{
void Show();
void Hide();
void UpdateMessage(String message);
void UpdateSubMessage(String submessage);
}
public interface IProgressUiProvider
{
IProgressUi? ProgressUi { get; }
}
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;
}
private string _lastMessage;
private string _lastSubMessage;
public void UpdateMessage(string message)
{
_lastMessage = message;
}
public void UpdateSubMessage(string submessage)
{
_lastSubMessage = submessage;
}
public bool ContinueWork()
{
return true;
}
public void UpdateMessage(UiStringKey stringKey)
{
}
public string LastMessage { get { return _lastMessage; } }
public string LastSubMessage { get { return _lastSubMessage; } }
}
/// <summary> /// <summary>
/// StatusLogger implementation which shows the progress in a progress dialog /// StatusLogger implementation which shows the progress in a progress dialog
/// </summary> /// </summary>
public class ProgressDialogStatusLogger: IStatusLogger { public class ProgressDialogUi: IProgressUi
{
private readonly IProgressDialog _progressDialog; private readonly IProgressDialog _progressDialog;
readonly IKp2aApp _app;
private readonly Handler _handler; private readonly Handler _handler;
private string _message = ""; private string _message = "";
private string _submessage; private string _submessage;
private readonly IKp2aApp _app;
public String SubMessage => _submessage; public String LastSubMessage => _submessage;
public String Message => _message; public String LastMessage => _message;
public ProgressDialogStatusLogger() {
} public ProgressDialogUi(IKp2aApp app, Handler handler, IProgressDialog pd)
{
public ProgressDialogStatusLogger(IKp2aApp app, Handler handler, IProgressDialog pd) {
_app = app; _app = app;
_progressDialog = pd; _progressDialog = pd;
_handler = handler; _handler = handler;
} }
public void UpdateMessage(UiStringKey stringKey) {
if (_app != null)
UpdateMessage(_app.GetResourceString(stringKey));
}
public void UpdateMessage (String message)
{
Kp2aLog.Log("status message: " + message);
_message = message;
if ( _app!= null && _progressDialog != null && _handler != null ) {
_handler.Post(() => {_progressDialog.SetMessage(message); } );
}
}
public void UpdateSubMessage(String submessage) public void UpdateSubMessage(String submessage)
{ {
Kp2aLog.Log("status submessage: " + submessage); Kp2aLog.Log("status submessage: " + submessage);
@@ -80,58 +134,40 @@ namespace keepass2android
} }
} }
#region IStatusLogger implementation public void Show()
public void StartLogging (string strOperation, bool bWriteOperationToLog)
{ {
_handler.Post(() =>
}
public void EndLogging ()
{ {
_progressDialog?.Show();
});
}
public void Hide()
{
_handler.Post(() =>
{
_progressDialog?.Dismiss();
});
}
public void UpdateMessage(string message)
{
Kp2aLog.Log("status message: " + message);
_message = message;
if (_app != null && _progressDialog != null && _handler != null)
{
_handler.Post(() =>
{
_progressDialog.SetMessage(message);
});
}
}
} }
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

@@ -1,179 +0,0 @@
/*
This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file is based on Keepassdroid, Copyright Brian Pellin.
Keepass2Android 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 2 of the License, or
(at your option) any later version.
Keepass2Android 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 Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/
using Android.App;
using Android.Content;
using Android.OS;
using Java.Lang;
namespace keepass2android
{
/// <summary>
/// Class to run a task while a progress dialog is shown
/// </summary>
public class ProgressTask
{
//for handling Activity recreation situations, we need access to the currently active task. It must hold that there is no more than one active task.
private static ProgressTask _currentTask = null;
public static void SetNewActiveActivity(Activity activeActivity)
{
if (_currentTask != null)
{
_currentTask.ActiveActivity = activeActivity;
}
}
public static void RemoveActiveActivity(Activity activity)
{
if ((_currentTask != null) && (_currentTask._activeActivity == activity))
_currentTask.ActiveActivity = null;
}
public Activity ActiveActivity
{
get { return _activeActivity; }
private set
{
if (_activeActivity != null && _activeActivity != _previouslyActiveActivity)
{
_previouslyActiveActivity = _activeActivity;
}
_activeActivity = value;
if (_task != null)
_task.ActiveActivity = _activeActivity;
if (_activeActivity != null)
{
SetupProgressDialog(_app);
_progressDialog.Show();
}
}
}
public Activity PreviouslyActiveActivity
{
get { return _previouslyActiveActivity; }
}
private readonly Handler _handler;
private readonly RunnableOnFinish _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)
{
_activeActivity = activity;
_task = task;
_handler = app.UiThreadHandler;
_app = app;
SetupProgressDialog(app);
// Set code to run when this is finished
_task.OnFinishToRun = new AfterTask(activity, task.OnFinishToRun, _handler, this);
_task.SetStatusLogger(_progressDialogStatusLogger);
}
private void SetupProgressDialog(IKp2aApp app)
{
string currentMessage = "Initializing...";
string currentSubmessage = "";
if (_progressDialogStatusLogger != null)
{
currentMessage = _progressDialogStatusLogger.Message;
currentSubmessage = _progressDialogStatusLogger.SubMessage;
}
if (_progressDialog != null)
{
var pd = _progressDialog;
app.UiThreadHandler.Post(() =>
{
pd.Dismiss();
});
}
// Show process dialog
_progressDialog = app.CreateProgressDialog(_activeActivity);
_progressDialog.SetTitle(_app.GetResourceString(UiStringKey.progress_title));
_progressDialogStatusLogger = new ProgressDialogStatusLogger(_app, _handler, _progressDialog);
_progressDialogStatusLogger.UpdateMessage(currentMessage);
_progressDialogStatusLogger.UpdateSubMessage(currentSubmessage);
}
public void Run(bool allowOverwriteCurrentTask = false)
{
if ((!allowOverwriteCurrentTask) && (_currentTask != null))
throw new System.Exception("Cannot start another ProgressTask while ProgressTask is already running! " + _task.GetType().Name + "/" + _currentTask._task.GetType().Name);
_currentTask = this;
// Show process dialog
_progressDialog.Show();
// Start Thread to Run task
_thread = new Java.Lang.Thread(_task.Run);
_thread.Start();
}
public void JoinWorkerThread()
{
_thread.Join();
}
private class AfterTask : OnFinish {
readonly ProgressTask _progressTask;
public AfterTask (Activity activity, OnFinish finish, Handler handler, ProgressTask pt): base(activity, finish, handler)
{
_progressTask = pt;
}
public override void Run() {
base.Run();
if (Handler != null) //can be null in tests
{
// Remove the progress dialog
Handler.Post(delegate
{
_progressTask._progressDialog.Dismiss();
});
}
else
{
_progressTask._progressDialog.Dismiss();
}
_currentTask = null;
}
}
}
}

View File

@@ -0,0 +1,94 @@
using KeePassLib.Interfaces;
namespace keepass2android;
public class ProgressUiAsStatusLoggerAdapter : IKp2aStatusLogger
{
private IProgressUi? _progressUi;
private readonly IKp2aApp _app;
private string _lastMessage = "";
private string _lastSubMessage = "";
private bool _isVisible = false;
public ProgressUiAsStatusLoggerAdapter(IProgressUi progressUi, IKp2aApp app)
{
_progressUi = progressUi;
_app = app;
}
public void SetNewProgressUi(IProgressUi progressUi)
{
_progressUi?.Hide();
_progressUi = progressUi;
if (_isVisible)
{
progressUi?.Show();
progressUi?.UpdateMessage(_lastMessage);
progressUi?.UpdateSubMessage(_lastSubMessage);
}
else
{
progressUi?.Hide();
}
}
public void StartLogging(string strOperation, bool bWriteOperationToLog)
{
_progressUi?.Show();
_isVisible = true;
}
public void EndLogging()
{
_progressUi?.Hide();
_isVisible = false;
}
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));
return true;
}
}
UpdateMessage(strNewText);
return true;
}
public void UpdateMessage(string message)
{
_progressUi?.UpdateMessage(message);
_lastMessage = message;
}
public void UpdateSubMessage(string submessage)
{
_progressUi?.UpdateSubMessage(submessage);
_lastSubMessage = submessage;
}
public bool ContinueWork()
{
return !Java.Lang.Thread.Interrupted();
}
public void UpdateMessage(UiStringKey stringKey)
{
if (_app != null)
UpdateMessage(_app.GetResourceString(stringKey));
}
public string LastMessage { get { return _lastMessage; } }
public string LastSubMessage { get { return _lastSubMessage; } }
}

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -15,7 +16,7 @@ namespace KeePass.Util
string errorMessage = e.Message; string errorMessage = e.Message;
if (e is Java.Lang.Exception javaException) if (e is Java.Lang.Exception javaException)
{ {
errorMessage = javaException.LocalizedMessage ?? javaException.Message ?? errorMessage; errorMessage = javaException.Message ?? errorMessage;
} }
return errorMessage; return errorMessage;

View File

@@ -13,16 +13,15 @@ using keepass2android.Io;
namespace keepass2android namespace keepass2android
{ {
public class CheckDatabaseForChanges: RunnableOnFinish public class CheckDatabaseForChanges: OperationWithFinishHandler
{ {
private readonly Context _context; private readonly Context _context;
private readonly IKp2aApp _app; private readonly IKp2aApp _app;
public CheckDatabaseForChanges(Activity context, IKp2aApp app, OnFinish finish) public CheckDatabaseForChanges(IKp2aApp app, OnOperationFinishedHandler operationFinishedHandler)
: base(context, finish) : base(app, operationFinishedHandler)
{ {
_context = context;
_app = app; _app = app;
} }

View File

@@ -85,7 +85,7 @@ namespace keepass2android
/// <summary> /// <summary>
/// Do not call this method directly. Call App.Kp2a.LoadDatabase instead. /// Do not call this method directly. Call App.Kp2a.LoadDatabase instead.
/// </summary> /// </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(); PwDatabase pwDatabase = new PwDatabase();
@@ -149,7 +149,7 @@ namespace keepass2android
get { return GetFingerprintModePrefKey(Ioc); } 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); IFileStorage fileStorage = _app.GetFileStorage(iocInfo);
var filename = fileStorage.GetFilenameWithoutPathAndExt(iocInfo); var filename = fileStorage.GetFilenameWithoutPathAndExt(iocInfo);
@@ -194,9 +194,9 @@ namespace keepass2android
} }
public void SaveData() { public void SaveData(IFileStorage fileStorage) {
using (IWriteTransaction trans = _app.GetFileStorage(Ioc).OpenWriteTransaction(Ioc, _app.GetBooleanPreference(PreferenceKey.UseFileTransactions))) using (IWriteTransaction trans = fileStorage.OpenWriteTransaction(Ioc, _app.GetBooleanPreference(PreferenceKey.UseFileTransactions)))
{ {
DatabaseFormat.Save(KpDatabase, trans.OpenFile()); DatabaseFormat.Save(KpDatabase, trans.OpenFile());

View File

@@ -396,8 +396,6 @@ namespace keepass2android
{ {
PwGroupV3 toGroup = new PwGroupV3(); PwGroupV3 toGroup = new PwGroupV3();
toGroup.Name = fromGroup.Name; toGroup.Name = fromGroup.Name;
//todo remove
Android.Util.Log.Debug("KP2A", "save kdb: group " + fromGroup.Name);
toGroup.TCreation = new PwDate(ConvertTime(fromGroup.CreationTime)); toGroup.TCreation = new PwDate(ConvertTime(fromGroup.CreationTime));
toGroup.TLastAccess= new PwDate(ConvertTime(fromGroup.LastAccessTime)); toGroup.TLastAccess= new PwDate(ConvertTime(fromGroup.LastAccessTime));

View File

@@ -4,37 +4,43 @@ using System.IO;
using System.Text; using System.Text;
using Android.App; using Android.App;
using Android.Content; using Android.Content;
using Android.OS;
using KeePassLib.Serialization; using KeePassLib.Serialization;
using keepass2android.Io; using keepass2android.Io;
using KeePass.Util; using KeePass.Util;
using Group.Pals.Android.Lib.UI.Filechooser.Utils;
using KeePassLib;
namespace keepass2android namespace keepass2android
{ {
public class SynchronizeCachedDatabase: RunnableOnFinish public class SynchronizeCachedDatabase: OperationWithFinishHandler
{ {
private readonly Activity _context;
private readonly IKp2aApp _app; private readonly IKp2aApp _app;
private SaveDb _saveDb; private IDatabaseModificationWatcher _modificationWatcher;
private readonly Database _database;
public SynchronizeCachedDatabase(Activity context, IKp2aApp app, OnFinish finish)
: base(context, finish) public SynchronizeCachedDatabase(IKp2aApp app, Database database, OnOperationFinishedHandler operationFinishedHandler, IDatabaseModificationWatcher modificationWatcher)
: base(app, operationFinishedHandler)
{ {
_context = context;
_app = app; _app = app;
_database = database;
_modificationWatcher = modificationWatcher;
} }
public override void Run() public override void Run()
{ {
try try
{ {
IOConnectionInfo ioc = _app.CurrentDb.Ioc; IOConnectionInfo ioc = _database.Ioc;
IFileStorage fileStorage = _app.GetFileStorage(ioc); IFileStorage fileStorage = _app.GetFileStorage(ioc);
if (!(fileStorage is CachingFileStorage)) if (!(fileStorage is CachingFileStorage))
{ {
throw new Exception("Cannot sync a non-cached database!"); throw new Exception("Cannot sync a non-cached database!");
} }
StatusLogger.UpdateMessage(UiStringKey.SynchronizingCachedDatabase); StatusLogger.UpdateMessage(UiStringKey.SynchronizingCachedDatabase);
CachingFileStorage cachingFileStorage = (CachingFileStorage) fileStorage; CachingFileStorage cachingFileStorage = (CachingFileStorage)fileStorage;
//download file from remote location and calculate hash: //download file from remote location and calculate hash:
StatusLogger.UpdateSubMessage(_app.GetResourceString(UiStringKey.DownloadingRemoteFile)); StatusLogger.UpdateSubMessage(_app.GetResourceString(UiStringKey.DownloadingRemoteFile));
@@ -49,7 +55,8 @@ namespace keepass2android
catch (FileNotFoundException) catch (FileNotFoundException)
{ {
StatusLogger.UpdateSubMessage(_app.GetResourceString(UiStringKey.RestoringRemoteFile)); StatusLogger.UpdateSubMessage(_app.GetResourceString(UiStringKey.RestoringRemoteFile));
cachingFileStorage.UpdateRemoteFile(ioc, _app.GetBooleanPreference(PreferenceKey.UseFileTransactions)); cachingFileStorage.UpdateRemoteFile(ioc,
_app.GetBooleanPreference(PreferenceKey.UseFileTransactions));
Finish(true, _app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully)); Finish(true, _app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully));
Kp2aLog.Log("Checking for file change: file not found"); Kp2aLog.Log("Checking for file change: file not found");
return; return;
@@ -64,7 +71,8 @@ namespace keepass2android
if (cachingFileStorage.HasLocalChanges(ioc)) if (cachingFileStorage.HasLocalChanges(ioc))
{ {
//conflict! need to merge //conflict! need to merge
_saveDb = new SaveDb(_context, _app, new ActionOnFinish(ActiveActivity, (success, result, activity) => var _saveDb = new SaveDb(_app, new ActionOnOperationFinished(_app,
(success, result, activity) =>
{ {
if (!success) if (!success)
{ {
@@ -74,19 +82,43 @@ namespace keepass2android
{ {
Finish(true, _app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully)); Finish(true, _app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully));
} }
_saveDb = null; }), _database, false, remoteData, _modificationWatcher);
}), _app.CurrentDb, false, remoteData); _saveDb.SetStatusLogger(StatusLogger);
_saveDb.DoNotSetStatusLoggerMessage = true; //Keep "sync db" as main message
_saveDb.SyncInBackground = false;
_saveDb.Run(); _saveDb.Run();
_app.CurrentDb.UpdateGlobals(); _database.UpdateGlobals();
_app.MarkAllGroupsAsDirty(); _app.MarkAllGroupsAsDirty();
} }
else else
{ {
//only the remote file was modified -> reload database. //only the remote file was modified -> reload database.
//note: it's best to lock the database and do a complete reload here (also better for UI consistency in case something goes wrong etc.) var onFinished = new ActionOnOperationFinished(_app, (success, result, activity) =>
_app.TriggerReload(_context, (bool result) => Finish(result)); {
if (!success)
{
Finish(false, result);
}
else
{
new Handler(Looper.MainLooper).Post(() =>
{
_database.UpdateGlobals();
_app.MarkAllGroupsAsDirty();
Finish(true, _app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully));
});
}
});
var _loadDb = new LoadDb(_app, ioc, Task.FromResult(remoteData),
_database.KpDatabase.MasterKey, null, onFinished, true, false, _modificationWatcher);
_loadDb.SetStatusLogger(StatusLogger);
_loadDb.DoNotSetStatusLoggerMessage = true; //Keep "sync db" as main message
_loadDb.Run();
} }
} }
else else
@@ -96,7 +128,8 @@ namespace keepass2android
{ {
//but we have local changes -> upload: //but we have local changes -> upload:
StatusLogger.UpdateSubMessage(_app.GetResourceString(UiStringKey.UploadingFile)); StatusLogger.UpdateSubMessage(_app.GetResourceString(UiStringKey.UploadingFile));
cachingFileStorage.UpdateRemoteFile(ioc, _app.GetBooleanPreference(PreferenceKey.UseFileTransactions)); cachingFileStorage.UpdateRemoteFile(ioc,
_app.GetBooleanPreference(PreferenceKey.UseFileTransactions));
StatusLogger.UpdateSubMessage(""); StatusLogger.UpdateSubMessage("");
Finish(true, _app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully)); Finish(true, _app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully));
} }
@@ -106,6 +139,16 @@ namespace keepass2android
Finish(true, _app.GetResourceString(UiStringKey.FilesInSync)); Finish(true, _app.GetResourceString(UiStringKey.FilesInSync));
} }
} }
}
catch (Java.Lang.InterruptedException e)
{
Kp2aLog.LogUnexpectedError(e);
//no Finish()
}
catch (Java.IO.InterruptedIOException e)
{
Kp2aLog.LogUnexpectedError(e);
//no Finish()
} }
catch (Exception e) catch (Exception e)
{ {
@@ -115,10 +158,5 @@ namespace keepass2android
} }
public void JoinWorkerThread()
{
if (_saveDb != null)
_saveDb.JoinWorkerThread();
}
} }
} }

View File

@@ -1,57 +0,0 @@
/*
This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll.
Keepass2Android 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 2 of the License, or
(at your option) any later version.
Keepass2Android 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 Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using Android.App;
using Android.OS;
namespace keepass2android
{
public class ActionOnFinish: OnFinish
{
public delegate void ActionToPerformOnFinsh(bool success, String message, Activity activeActivity);
readonly ActionToPerformOnFinsh _actionToPerform;
public ActionOnFinish(Activity activity, ActionToPerformOnFinsh actionToPerform) : base(activity, null, null)
{
_actionToPerform = actionToPerform;
}
public ActionOnFinish(Activity activity, ActionToPerformOnFinsh actionToPerform, OnFinish finish) : base(activity, finish)
{
_actionToPerform = actionToPerform;
}
//if set to true, the previously active active will be passed to ActionToPerformOnFinish instead null if no activity is on foreground
public bool AllowInactiveActivity { get; set; }
public override void Run()
{
if (Message == null)
Message = "";
if (Handler != null)
{
Handler.Post(() => {_actionToPerform(Success, Message, ActiveActivity);});
}
else
_actionToPerform(Success, Message, AllowInactiveActivity ? (ActiveActivity ?? PreviouslyActiveActivity) : ActiveActivity);
base.Run();
}
}
}

View File

@@ -0,0 +1,92 @@
/*
This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll.
Keepass2Android 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 2 of the License, or
(at your option) any later version.
Keepass2Android 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 Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using Android.App;
using Android.Content;
using Android.OS;
using keepass2android;
namespace keepass2android
{
public class ActionOnOperationFinished: OnOperationFinishedHandler
{
public delegate void ActionToPerformOnFinsh(bool success, String message, Context activeContext);
readonly ActionToPerformOnFinsh _actionToPerform;
public ActionOnOperationFinished(IKp2aApp app, ActionToPerformOnFinsh actionToPerform) : base(app, null, null)
{
_actionToPerform = actionToPerform;
}
public ActionOnOperationFinished(IKp2aApp app, ActionToPerformOnFinsh actionToPerform, OnOperationFinishedHandler operationFinishedHandler) : base(app, operationFinishedHandler)
{
_actionToPerform = actionToPerform;
}
public override void Run()
{
if (Message == null)
Message = "";
if (Handler != null)
{
Handler.Post(() =>
{
_actionToPerform(Success, Message, ActiveContext);
});
}
else
{
_actionToPerform(Success, Message, ActiveContext);
}
base.Run();
}
}
}
//Action which runs when the contextInstanceId is the active context
// otherwise it is registered as pending action for the context instance.
public class ActionInContextInstanceOnOperationFinished : ActionOnOperationFinished
{
private readonly int _contextInstanceId;
private IKp2aApp _app;
public ActionInContextInstanceOnOperationFinished(int contextInstanceId, IKp2aApp app, ActionToPerformOnFinsh actionToPerform) : base(app, actionToPerform)
{
_contextInstanceId = contextInstanceId;
_app = app;
}
public ActionInContextInstanceOnOperationFinished(int contextInstanceId, IKp2aApp app, ActionToPerformOnFinsh actionToPerform, OnOperationFinishedHandler operationFinishedHandler) : base(app, actionToPerform, operationFinishedHandler)
{
_contextInstanceId = contextInstanceId;
_app = app;
}
public override void Run()
{
if ((ActiveContext as IContextInstanceIdProvider)?.ContextInstanceId != _contextInstanceId)
{
_app.RegisterPendingActionForContextInstance(_contextInstanceId, this);
}
else _app.UiThreadHandler.Post(() => base.Run());
}
}

View File

@@ -21,7 +21,7 @@ using KeePassLib;
namespace keepass2android namespace keepass2android
{ {
public class AddEntry : RunnableOnFinish { public class AddEntry : OperationWithFinishHandler {
protected Database Db protected Database Db
{ {
get { return _app.CurrentDb; } get { return _app.CurrentDb; }
@@ -30,22 +30,20 @@ namespace keepass2android
private readonly IKp2aApp _app; private readonly IKp2aApp _app;
private readonly PwEntry _entry; private readonly PwEntry _entry;
private readonly PwGroup _parentGroup; private readonly PwGroup _parentGroup;
private readonly Activity _ctx;
private readonly Database _db; private readonly Database _db;
public static AddEntry GetInstance(Activity ctx, IKp2aApp app, PwEntry entry, PwGroup parentGroup, OnFinish finish, Database db) { public static AddEntry GetInstance(IKp2aApp app, PwEntry entry, PwGroup parentGroup, OnOperationFinishedHandler operationFinishedHandler, Database db) {
return new AddEntry(ctx, db, app, entry, parentGroup, finish); return new AddEntry(db, app, entry, parentGroup, operationFinishedHandler);
} }
public AddEntry(Activity ctx, Database db, IKp2aApp app, PwEntry entry, PwGroup parentGroup, OnFinish finish):base(ctx, finish) { public AddEntry(Database db, IKp2aApp app, PwEntry entry, PwGroup parentGroup, OnOperationFinishedHandler operationFinishedHandler):base(app, operationFinishedHandler) {
_ctx = ctx;
_db = db; _db = db;
_parentGroup = parentGroup; _parentGroup = parentGroup;
_app = app; _app = app;
_entry = entry; _entry = entry;
_onFinishToRun = new AfterAdd(ctx, app.CurrentDb, entry, app,OnFinishToRun); _operationFinishedHandler = new AfterAdd(app.CurrentDb, entry, app,operationFinishedHandler);
} }
@@ -65,17 +63,17 @@ namespace keepass2android
_db.Elements.Add(_entry); _db.Elements.Add(_entry);
// Commit to disk // Commit to disk
SaveDb save = new SaveDb(_ctx, _app, _app.CurrentDb, OnFinishToRun); SaveDb save = new SaveDb(_app, _app.CurrentDb, operationFinishedHandler);
save.SetStatusLogger(StatusLogger); save.SetStatusLogger(StatusLogger);
save.Run(); save.Run();
} }
private class AfterAdd : OnFinish { private class AfterAdd : OnOperationFinishedHandler {
private readonly Database _db; private readonly Database _db;
private readonly PwEntry _entry; private readonly PwEntry _entry;
private readonly IKp2aApp _app; private readonly IKp2aApp _app;
public AfterAdd(Activity activity, Database db, PwEntry entry, IKp2aApp app, OnFinish finish):base(activity, finish) { public AfterAdd( Database db, PwEntry entry, IKp2aApp app, OnOperationFinishedHandler operationFinishedHandler):base(app, operationFinishedHandler) {
_db = db; _db = db;
_entry = entry; _entry = entry;
_app = app; _app = app;

View File

@@ -23,7 +23,7 @@ using KeePassLib;
namespace keepass2android namespace keepass2android
{ {
public class AddGroup : RunnableOnFinish { public class AddGroup : OperationWithFinishHandler {
internal Database Db internal Database Db
{ {
get { return _app.CurrentDb; } get { return _app.CurrentDb; }
@@ -38,18 +38,16 @@ namespace keepass2android
public PwGroup Group; public PwGroup Group;
internal PwGroup Parent; internal PwGroup Parent;
protected bool DontSave; protected bool DontSave;
readonly Activity _ctx;
public static AddGroup GetInstance(Activity ctx, IKp2aApp app, string name, int iconid, PwUuid groupCustomIconId, PwGroup parent, OnFinish finish, bool dontSave) { public static AddGroup GetInstance(IKp2aApp app, string name, int iconid, PwUuid groupCustomIconId, PwGroup parent, OnOperationFinishedHandler operationFinishedHandler, bool dontSave) {
return new AddGroup(ctx, app, name, iconid, groupCustomIconId, parent, finish, dontSave); return new AddGroup(app, name, iconid, groupCustomIconId, parent, operationFinishedHandler, dontSave);
} }
private AddGroup(Activity ctx, IKp2aApp app, String name, int iconid, PwUuid groupCustomIconId, PwGroup parent, OnFinish finish, bool dontSave) private AddGroup(IKp2aApp app, String name, int iconid, PwUuid groupCustomIconId, PwGroup parent, OnOperationFinishedHandler operationFinishedHandler, bool dontSave)
: base(ctx, finish) : base(app, operationFinishedHandler)
{ {
_ctx = ctx;
_name = name; _name = name;
_iconId = iconid; _iconId = iconid;
_groupCustomIconId = groupCustomIconId; _groupCustomIconId = groupCustomIconId;
@@ -57,7 +55,7 @@ namespace keepass2android
DontSave = dontSave; DontSave = dontSave;
_app = app; _app = app;
_onFinishToRun = new AfterAdd(ctx, this, OnFinishToRun); _operationFinishedHandler = new AfterAdd(_app, this, operationFinishedHandler);
} }
@@ -74,15 +72,15 @@ namespace keepass2android
_app.CurrentDb.Elements.Add(Group); _app.CurrentDb.Elements.Add(Group);
// Commit to disk // Commit to disk
SaveDb save = new SaveDb(_ctx, _app, _app.CurrentDb, OnFinishToRun, DontSave); SaveDb save = new SaveDb(_app, _app.CurrentDb, operationFinishedHandler, DontSave, null);
save.SetStatusLogger(StatusLogger); save.SetStatusLogger(StatusLogger);
save.Run(); save.Run();
} }
private class AfterAdd : OnFinish { private class AfterAdd : OnOperationFinishedHandler {
readonly AddGroup _addGroup; readonly AddGroup _addGroup;
public AfterAdd(Activity activity, AddGroup addGroup,OnFinish finish): base(activity, finish) { public AfterAdd(IKp2aApp app, AddGroup addGroup,OnOperationFinishedHandler operationFinishedHandler): base(app, operationFinishedHandler) {
_addGroup = addGroup; _addGroup = addGroup;
} }

View File

@@ -26,7 +26,7 @@ using KeePassLib.Utility;
namespace keepass2android namespace keepass2android
{ {
public class AddTemplateEntries : RunnableOnFinish { public class AddTemplateEntries : OperationWithFinishHandler {
public class TemplateEntry public class TemplateEntry
{ {
@@ -130,15 +130,13 @@ namespace keepass2android
} }
private readonly IKp2aApp _app; private readonly IKp2aApp _app;
private readonly Activity _ctx;
public AddTemplateEntries(Activity ctx, IKp2aApp app, OnFinish finish) public AddTemplateEntries(IKp2aApp app, OnOperationFinishedHandler operationFinishedHandler)
: base(ctx, finish) : base(app, operationFinishedHandler)
{ {
_ctx = ctx;
_app = app; _app = app;
//_onFinishToRun = new AfterAdd(this, OnFinishToRun); //_operationFinishedHandler = new AfterAdd(this, operationFinishedHandler);
} }
public static readonly List<TemplateEntry> TemplateEntries = new List<TemplateEntry>() public static readonly List<TemplateEntry> TemplateEntries = new List<TemplateEntry>()
@@ -313,7 +311,7 @@ namespace keepass2android
_app.DirtyGroups.Add(templateGroup); _app.DirtyGroups.Add(templateGroup);
// Commit to disk // Commit to disk
SaveDb save = new SaveDb(_ctx, _app, _app.CurrentDb, OnFinishToRun); SaveDb save = new SaveDb(_app, _app.CurrentDb, operationFinishedHandler);
save.SetStatusLogger(StatusLogger); save.SetStatusLogger(StatusLogger);
save.Run(); save.Run();
} }
@@ -337,7 +335,6 @@ namespace keepass2android
_app.DirtyGroups.Add(_app.CurrentDb.KpDatabase.RootGroup); _app.DirtyGroups.Add(_app.CurrentDb.KpDatabase.RootGroup);
_app.CurrentDb.GroupsById[templateGroup.Uuid] = templateGroup; _app.CurrentDb.GroupsById[templateGroup.Uuid] = templateGroup;
_app.CurrentDb.Elements.Add(templateGroup); _app.CurrentDb.Elements.Add(templateGroup);
} }
addedEntries = new List<PwEntry>(); addedEntries = new List<PwEntry>();
@@ -369,11 +366,11 @@ namespace keepass2android
return entry; return entry;
} }
private class AfterAdd : OnFinish { private class AfterAdd : OnOperationFinishedHandler {
private readonly Database _db; private readonly Database _db;
private readonly List<PwEntry> _entries; private readonly List<PwEntry> _entries;
public AfterAdd(Activity activity, Database db, List<PwEntry> entries, OnFinish finish):base(activity, finish) { public AfterAdd(IKp2aApp app, Database db, List<PwEntry> entries, OnOperationFinishedHandler operationFinishedHandler):base(app, operationFinishedHandler) {
_db = db; _db = db;
_entries = entries; _entries = entries;

View File

@@ -16,8 +16,8 @@ namespace keepass2android.database.edit
{ {
public class CopyEntry: AddEntry public class CopyEntry: AddEntry
{ {
public CopyEntry(Activity ctx, IKp2aApp app, PwEntry entry, OnFinish finish, Database db) public CopyEntry(IKp2aApp app, PwEntry entry, OnOperationFinishedHandler operationFinishedHandler, Database db)
: base(ctx, db, app, CreateCopy(entry, app), entry.ParentGroup, finish) : base(db, app, CreateCopy(entry, app), entry.ParentGroup, operationFinishedHandler)
{ {
} }

View File

@@ -26,26 +26,23 @@ using KeePassLib.Keys;
namespace keepass2android namespace keepass2android
{ {
public class CreateDb : RunnableOnFinish { public class CreateDb : OperationWithFinishHandler {
private readonly IOConnectionInfo _ioc; private readonly IOConnectionInfo _ioc;
private readonly bool _dontSave; private readonly bool _dontSave;
private readonly Activity _ctx;
private readonly IKp2aApp _app; private readonly IKp2aApp _app;
private CompositeKey _key; private CompositeKey _key;
private readonly bool _makeCurrent; private readonly bool _makeCurrent;
public CreateDb(IKp2aApp app, Activity ctx, IOConnectionInfo ioc, OnFinish finish, bool dontSave, bool makeCurrent): base(ctx, finish) { public CreateDb(IKp2aApp app, Activity ctx, IOConnectionInfo ioc, OnOperationFinishedHandler operationFinishedHandler, bool dontSave, bool makeCurrent): base(app, operationFinishedHandler) {
_ctx = ctx;
_ioc = ioc; _ioc = ioc;
_dontSave = dontSave; _dontSave = dontSave;
_makeCurrent = makeCurrent; _makeCurrent = makeCurrent;
_app = app; _app = app;
} }
public CreateDb(IKp2aApp app, Activity ctx, IOConnectionInfo ioc, OnFinish finish, bool dontSave, CompositeKey key, bool makeCurrent) public CreateDb(IKp2aApp app, Activity ctx, IOConnectionInfo ioc, OnOperationFinishedHandler operationFinishedHandler, bool dontSave, CompositeKey key, bool makeCurrent)
: base(ctx, finish) : base(app, operationFinishedHandler)
{ {
_ctx = ctx;
_ioc = ioc; _ioc = ioc;
_dontSave = dontSave; _dontSave = dontSave;
_app = app; _app = app;
@@ -77,19 +74,19 @@ namespace keepass2android
db.SearchHelper = new SearchDbHelper(_app); db.SearchHelper = new SearchDbHelper(_app);
// Add a couple default groups // Add a couple default groups
AddGroup internet = AddGroup.GetInstance(_ctx, _app, "Internet", 1, null, db.KpDatabase.RootGroup, null, true); AddGroup internet = AddGroup.GetInstance(_app, "Internet", 1, null, db.KpDatabase.RootGroup, null, true);
internet.Run(); internet.Run();
AddGroup email = AddGroup.GetInstance(_ctx, _app, "eMail", 19, null, db.KpDatabase.RootGroup, null, true); AddGroup email = AddGroup.GetInstance(_app, "eMail", 19, null, db.KpDatabase.RootGroup, null, true);
email.Run(); email.Run();
List<PwEntry> addedEntries; List<PwEntry> addedEntries;
AddTemplateEntries addTemplates = new AddTemplateEntries(_ctx, _app, null); AddTemplateEntries addTemplates = new AddTemplateEntries(_app, null);
addTemplates.AddTemplates(out addedEntries); addTemplates.AddTemplates(out addedEntries);
// Commit changes // Commit changes
SaveDb save = new SaveDb(_ctx, _app, db, OnFinishToRun, _dontSave); SaveDb save = new SaveDb(_app, db, operationFinishedHandler, _dontSave, null);
save.SetStatusLogger(StatusLogger); save.SetStatusLogger(StatusLogger);
_onFinishToRun = null; _operationFinishedHandler = null;
save.Run(); save.Run();
db.UpdateGlobals(); db.UpdateGlobals();

View File

@@ -0,0 +1,39 @@
using Java.Lang;
namespace keepass2android;
public interface IDatabaseModificationWatcher
{
void BeforeModifyDatabases();
void AfterModifyDatabases();
}
public class NullDatabaseModificationWatcher : IDatabaseModificationWatcher
{
public void BeforeModifyDatabases() { }
public void AfterModifyDatabases() { }
}
public class BackgroundDatabaseModificationLocker(IKp2aApp app) : IDatabaseModificationWatcher
{
public void BeforeModifyDatabases()
{
while (true)
{
if (app.DatabasesBackgroundModificationLock.TryEnterWriteLock(TimeSpan.FromSeconds(0.1)))
{
break;
}
if (Java.Lang.Thread.Interrupted())
{
throw new InterruptedException();
}
}
}
public void AfterModifyDatabases()
{
app.DatabasesBackgroundModificationLock.ExitWriteLock();
}
}

View File

@@ -29,8 +29,8 @@ namespace keepass2android
private readonly PwEntry _entry; private readonly PwEntry _entry;
private UiStringKey _statusMessage; private UiStringKey _statusMessage;
public DeleteEntry(Activity activiy, IKp2aApp app, PwEntry entry, OnFinish finish):base(activiy, finish, app) { public DeleteEntry(IKp2aApp app, PwEntry entry, OnOperationFinishedHandler operationFinishedHandler):base(operationFinishedHandler, app) {
Ctx = activiy;
Db = app.FindDatabaseForElement(entry); Db = app.FindDatabaseForElement(entry);
_entry = entry; _entry = entry;

View File

@@ -29,25 +29,25 @@ namespace keepass2android
private PwGroup _group; private PwGroup _group;
protected bool DontSave; protected bool DontSave;
public DeleteGroup(Activity activity, IKp2aApp app, PwGroup group, OnFinish finish) public DeleteGroup(Activity activity, IKp2aApp app, PwGroup group, OnOperationFinishedHandler operationFinishedHandler)
: base(activity, finish, app) : base(operationFinishedHandler, app)
{ {
SetMembers(activity, app, group, false); SetMembers(app, group, false);
} }
/* /*
public DeleteGroup(Context ctx, Database db, PwGroup group, Activity act, OnFinish finish, bool dontSave) public DeleteGroup(Context ctx, Database db, PwGroup group, Activity act, OnOperationFinishedHandler operationFinishedHandler, bool dontSave)
: base(finish) : base(operationFinishedHandler)
{ {
SetMembers(ctx, db, group, act, dontSave); SetMembers(ctx, db, group, act, dontSave);
} }
public DeleteGroup(Context ctx, Database db, PwGroup group, OnFinish finish, bool dontSave):base(finish) { public DeleteGroup(Context ctx, Database db, PwGroup group, OnOperationFinishedHandler operationFinishedHandler, bool dontSave):base(operationFinishedHandler) {
SetMembers(ctx, db, group, null, dontSave); SetMembers(ctx, db, group, null, dontSave);
} }
*/ */
private void SetMembers(Activity activity, IKp2aApp app, PwGroup group, bool dontSave) private void SetMembers(IKp2aApp app, PwGroup group, bool dontSave)
{ {
base.SetMembers(activity, app.FindDatabaseForElement(group)); base.SetMembers(app.FindDatabaseForElement(group));
_group = group; _group = group;
DontSave = dontSave; DontSave = dontSave;

View File

@@ -12,11 +12,11 @@ namespace keepass2android
private readonly List<IStructureItem> _elementsToDelete; private readonly List<IStructureItem> _elementsToDelete;
private readonly bool _canRecycle; private readonly bool _canRecycle;
public DeleteMultipleItemsFromOneDatabase(Activity activity, Database db, List<IStructureItem> elementsToDelete, OnFinish finish, IKp2aApp app) public DeleteMultipleItemsFromOneDatabase(Activity activity, Database db, List<IStructureItem> elementsToDelete, OnOperationFinishedHandler operationFinishedHandler, IKp2aApp app)
: base(activity, finish, app) : base(operationFinishedHandler, app)
{ {
_elementsToDelete = elementsToDelete; _elementsToDelete = elementsToDelete;
SetMembers(activity, db); SetMembers(db);
//determine once. The property is queried for each delete operation, but might return false //determine once. The property is queried for each delete operation, but might return false
//after one entry/group is deleted (and thus in recycle bin and thus can't be recycled anymore) //after one entry/group is deleted (and thus in recycle bin and thus can't be recycled anymore)

View File

@@ -6,10 +6,10 @@ using KeePassLib;
namespace keepass2android namespace keepass2android
{ {
public abstract class DeleteRunnable : RunnableOnFinish public abstract class DeleteRunnable : OperationWithFinishHandler
{ {
protected DeleteRunnable(Activity activity, OnFinish finish, IKp2aApp app) protected DeleteRunnable(OnOperationFinishedHandler operationFinishedHandler, IKp2aApp app)
: base(activity, finish) : base(app, operationFinishedHandler)
{ {
App = app; App = app;
} }
@@ -18,11 +18,10 @@ namespace keepass2android
protected Database Db; protected Database Db;
protected Activity Ctx;
protected void SetMembers(Activity activity, Database db)
protected void SetMembers( Database db)
{ {
Ctx = activity;
Db = db; Db = db;
} }
@@ -131,18 +130,18 @@ namespace keepass2android
(dlgSender, dlgEvt) => (dlgSender, dlgEvt) =>
{ {
DeletePermanently = true; DeletePermanently = true;
ProgressTask pt = new ProgressTask(App, Ctx, this); BlockingOperationStarter pt = new BlockingOperationStarter(App, this);
pt.Run(); pt.Run();
}, },
(dlgSender, dlgEvt) => (dlgSender, dlgEvt) =>
{ {
DeletePermanently = false; DeletePermanently = false;
ProgressTask pt = new ProgressTask(App, Ctx, this); BlockingOperationStarter pt = new BlockingOperationStarter(App, this);
pt.Run(); pt.Run();
}, },
(dlgSender, dlgEvt) => { }, (dlgSender, dlgEvt) => { },
Ctx, messageSuffix); messageSuffix);
@@ -153,12 +152,12 @@ namespace keepass2android
QuestionNoRecycleResourceId, QuestionNoRecycleResourceId,
(dlgSender, dlgEvt) => (dlgSender, dlgEvt) =>
{ {
ProgressTask pt = new ProgressTask(App, Ctx, this); BlockingOperationStarter pt = new BlockingOperationStarter(App, this);
pt.Run(); pt.Run();
}, },
null, null,
(dlgSender, dlgEvt) => { }, (dlgSender, dlgEvt) => { },
Ctx, messageSuffix); messageSuffix);
} }
@@ -215,7 +214,7 @@ namespace keepass2android
Android.Util.Log.Debug("KP2A", "Calling PerformDelete.."); Android.Util.Log.Debug("KP2A", "Calling PerformDelete..");
PerformDelete(touchedGroups, permanentlyDeletedGroups); PerformDelete(touchedGroups, permanentlyDeletedGroups);
_onFinishToRun = new ActionOnFinish(ActiveActivity,(success, message, activity) => _operationFinishedHandler = new ActionOnOperationFinished(App,(success, message, context) =>
{ {
if (success) if (success)
{ {
@@ -236,10 +235,10 @@ namespace keepass2android
// Let's not bother recovering from a failure to save. It is too much work. // Let's not bother recovering from a failure to save. It is too much work.
App.Lock(false, false); App.Lock(false, false);
} }
}, OnFinishToRun); }, operationFinishedHandler);
// Commit database // Commit database
SaveDb save = new SaveDb(Ctx, App, Db, OnFinishToRun, false); SaveDb save = new SaveDb( App, Db, operationFinishedHandler, false, null);
save.ShowDatabaseIocInStatus = ShowDatabaseIocInStatus; save.ShowDatabaseIocInStatus = ShowDatabaseIocInStatus;
save.SetStatusLogger(StatusLogger); save.SetStatusLogger(StatusLogger);

View File

@@ -23,7 +23,7 @@ using KeePassLib;
namespace keepass2android namespace keepass2android
{ {
public class EditGroup : RunnableOnFinish { public class EditGroup : OperationWithFinishHandler {
internal Database Db internal Database Db
{ {
get { return _app.FindDatabaseForElement(Group); } get { return _app.FindDatabaseForElement(Group); }
@@ -36,19 +36,17 @@ namespace keepass2android
private readonly PwIcon _iconId; private readonly PwIcon _iconId;
private readonly PwUuid _customIconId; private readonly PwUuid _customIconId;
internal PwGroup Group; internal PwGroup Group;
readonly Activity _ctx;
public EditGroup(Activity ctx, IKp2aApp app, String name, PwIcon iconid, PwUuid customIconId, PwGroup group, OnFinish finish) public EditGroup(IKp2aApp app, String name, PwIcon iconid, PwUuid customIconId, PwGroup group, OnOperationFinishedHandler operationFinishedHandler)
: base(ctx, finish) : base(app, operationFinishedHandler)
{ {
_ctx = ctx;
_name = name; _name = name;
_iconId = iconid; _iconId = iconid;
Group = group; Group = group;
_customIconId = customIconId; _customIconId = customIconId;
_app = app; _app = app;
_onFinishToRun = new AfterEdit(ctx, this, OnFinishToRun); _operationFinishedHandler = new AfterEdit(app, this, operationFinishedHandler);
} }
@@ -60,16 +58,16 @@ namespace keepass2android
Group.Touch(true); Group.Touch(true);
// Commit to disk // Commit to disk
SaveDb save = new SaveDb(_ctx, _app, Db, OnFinishToRun); SaveDb save = new SaveDb(_app, Db, operationFinishedHandler);
save.SetStatusLogger(StatusLogger); save.SetStatusLogger(StatusLogger);
save.Run(); save.Run();
} }
private class AfterEdit : OnFinish { private class AfterEdit : OnOperationFinishedHandler {
readonly EditGroup _editGroup; readonly EditGroup _editGroup;
public AfterEdit(Activity ctx, EditGroup editGroup, OnFinish finish) public AfterEdit(IKp2aApp app, EditGroup editGroup, OnOperationFinishedHandler operationFinishedHandler)
: base(ctx, finish) : base(app, operationFinishedHandler)
{ {
_editGroup = editGroup; _editGroup = editGroup;
} }

View File

@@ -21,10 +21,10 @@ using Android.App;
namespace keepass2android namespace keepass2android
{ {
public abstract class FileOnFinish : OnFinish { public abstract class FileOnFinish : OnOperationFinishedHandler {
private String _filename = ""; private String _filename = "";
protected FileOnFinish(Activity activity, FileOnFinish finish):base(activity, finish) { protected FileOnFinish(IKp2aApp app, FileOnFinish operationFinishedHandler):base(app, operationFinishedHandler) {
} }
public string Filename public string Filename

View File

@@ -0,0 +1,13 @@
namespace keepass2android;
// A context instance can be the instance of an Activity. Even if the activity is recreated (due to a configuration change, for example), the instance id must remain the same
// but it must be different for other activities/services or if the activity is finished and then starts again.
// We want to be able to perform actions on a context instance, even though that instance might not live at the time when we want to perform the action.
// In that case, we want to be able to register the action such that it is performed when the activity is recreated.
public interface IContextInstanceIdProvider
{
int ContextInstanceId { get; }
}

View File

@@ -21,25 +21,33 @@ using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Android.App; using Android.App;
using Android.OS;
using KeePass.Util; using KeePass.Util;
using keepass2android.database.edit; using keepass2android.database.edit;
using keepass2android.Io;
using KeePassLib; using KeePassLib;
using KeePassLib.Keys; using KeePassLib.Keys;
using KeePassLib.Serialization; using KeePassLib.Serialization;
namespace keepass2android namespace keepass2android
{ {
public class LoadDb : RunnableOnFinish { public class LoadDb : OperationWithFinishHandler {
private readonly IOConnectionInfo _ioc; private readonly IOConnectionInfo _ioc;
private readonly Task<MemoryStream> _databaseData; private readonly Task<MemoryStream> _databaseData;
private readonly CompositeKey _compositeKey; private readonly CompositeKey _compositeKey;
private readonly string _keyfileOrProvider; private readonly string? _keyfileOrProvider;
private readonly IKp2aApp _app; private readonly IKp2aApp _app;
private readonly bool _rememberKeyfile; private readonly bool _rememberKeyfile;
IDatabaseFormat _format; IDatabaseFormat _format;
public LoadDb(Activity activity, IKp2aApp app, IOConnectionInfo ioc, Task<MemoryStream> databaseData, CompositeKey compositeKey, String keyfileOrProvider, OnFinish finish, bool updateLastUsageTimestamp, bool makeCurrent): base(activity, finish) public bool DoNotSetStatusLoggerMessage = false;
public LoadDb(IKp2aApp app, IOConnectionInfo ioc, Task<MemoryStream> databaseData, CompositeKey compositeKey,
string keyfileOrProvider, OnOperationFinishedHandler operationFinishedHandler,
bool updateLastUsageTimestamp, bool makeCurrent, IDatabaseModificationWatcher modificationWatcher = null): base(app, operationFinishedHandler)
{ {
_modificationWatcher = modificationWatcher ?? new NullDatabaseModificationWatcher();
_app = app; _app = app;
_ioc = ioc; _ioc = ioc;
_databaseData = databaseData; _databaseData = databaseData;
@@ -47,14 +55,13 @@ namespace keepass2android
_keyfileOrProvider = keyfileOrProvider; _keyfileOrProvider = keyfileOrProvider;
_updateLastUsageTimestamp = updateLastUsageTimestamp; _updateLastUsageTimestamp = updateLastUsageTimestamp;
_makeCurrent = makeCurrent; _makeCurrent = makeCurrent;
_rememberKeyfile = app.GetBooleanPreference(PreferenceKey.remember_keyfile); _rememberKeyfile = app.GetBooleanPreference(PreferenceKey.remember_keyfile);
} }
protected bool success = false; protected bool success = false;
private bool _updateLastUsageTimestamp; private bool _updateLastUsageTimestamp;
private readonly bool _makeCurrent; private readonly bool _makeCurrent;
private readonly IDatabaseModificationWatcher _modificationWatcher;
public override void Run() public override void Run()
{ {
@@ -66,15 +73,36 @@ namespace keepass2android
SaveFileData(_ioc, _keyfileOrProvider); SaveFileData(_ioc, _keyfileOrProvider);
var fileStorage = _app.GetFileStorage(_ioc);
RequiresSubsequentSync = false;
if (!DoNotSetStatusLoggerMessage)
{
StatusLogger.UpdateMessage(UiStringKey.loading_database); StatusLogger.UpdateMessage(UiStringKey.loading_database);
}
//get the stream data into a single stream variable (databaseStream) regardless whether its preloaded or not: //get the stream data into a single stream variable (databaseStream) regardless whether its preloaded or not:
MemoryStream preloadedMemoryStream = _databaseData == null ? null : _databaseData.Result; MemoryStream preloadedMemoryStream = _databaseData == null ? null : _databaseData.Result;
MemoryStream databaseStream; MemoryStream databaseStream;
if (preloadedMemoryStream != null) if (preloadedMemoryStream != null)
{
//note: if the stream has been loaded already, we don't need to trigger another sync later on
databaseStream = preloadedMemoryStream; databaseStream = preloadedMemoryStream;
}
else else
{ {
using (Stream s = _app.GetFileStorage(_ioc).OpenFileForRead(_ioc)) if (_app.SyncInBackgroundPreference && fileStorage is CachingFileStorage cachingFileStorage &&
cachingFileStorage.IsCached(_ioc))
{
cachingFileStorage.IsOffline = true;
//no warning. We'll trigger a sync later.
cachingFileStorage.TriggerWarningWhenFallingBackToCache = false;
RequiresSubsequentSync = true;
}
using (Stream s = fileStorage.OpenFileForRead(_ioc))
{ {
databaseStream = new MemoryStream(); databaseStream = new MemoryStream();
s.CopyTo(databaseStream); s.CopyTo(databaseStream);
@@ -82,8 +110,13 @@ namespace keepass2android
} }
} }
if (!StatusLogger.ContinueWork())
{
return;
}
//ok, try to load the database. Let's start with Kdbx format and retry later if that is the wrong guess: //ok, try to load the database. Let's start with Kdbx format and retry later if that is the wrong guess:
_format = new KdbxDatabaseFormat(KdbxDatabaseFormat.GetFormatToUse(_app.GetFileStorage(_ioc).GetFileExtension(_ioc))); _format = new KdbxDatabaseFormat(KdbxDatabaseFormat.GetFormatToUse(fileStorage.GetFileExtension(_ioc)));
TryLoad(databaseStream); TryLoad(databaseStream);
@@ -120,6 +153,12 @@ namespace keepass2android
Finish(false, _app.GetResourceString(UiStringKey.DuplicateUuidsError) + " " + ExceptionUtil.GetErrorMessage(e) + _app.GetResourceString(UiStringKey.DuplicateUuidsErrorAdditional), false, Exception); Finish(false, _app.GetResourceString(UiStringKey.DuplicateUuidsError) + " " + ExceptionUtil.GetErrorMessage(e) + _app.GetResourceString(UiStringKey.DuplicateUuidsErrorAdditional), false, Exception);
return; return;
} }
catch (Java.Lang.InterruptedException)
{
Kp2aLog.Log("Load interrupted");
//close without Finish()
return;
}
catch (Exception e) catch (Exception e)
{ {
if (!(e is InvalidCompositeKeyException)) if (!(e is InvalidCompositeKeyException))
@@ -131,6 +170,8 @@ namespace keepass2android
} }
public bool RequiresSubsequentSync { get; set; } = false;
/// <summary> /// <summary>
/// Holds the exception which was thrown during execution (if any) /// Holds the exception which was thrown during execution (if any)
/// </summary> /// </summary>
@@ -146,10 +187,16 @@ namespace keepass2android
workingCopy.Seek(0, SeekOrigin.Begin); workingCopy.Seek(0, SeekOrigin.Begin);
//reset stream if we need to reuse it later: //reset stream if we need to reuse it later:
databaseStream.Seek(0, SeekOrigin.Begin); databaseStream.Seek(0, SeekOrigin.Begin);
if (!StatusLogger.ContinueWork())
{
throw new Java.Lang.InterruptedException();
}
//now let's go: //now let's go:
try try
{ {
Database newDb = _app.LoadDatabase(_ioc, workingCopy, _compositeKey, StatusLogger, _format, _makeCurrent); Database newDb =
_app.LoadDatabase(_ioc, workingCopy, _compositeKey, StatusLogger, _format, _makeCurrent, _modificationWatcher);
Kp2aLog.Log("LoadDB OK"); Kp2aLog.Log("LoadDB OK");
Finish(true, _format.SuccessMessage); Finish(true, _format.SuccessMessage);

View File

@@ -10,18 +10,16 @@ using KeePassLib.Interfaces;
namespace keepass2android.database.edit namespace keepass2android.database.edit
{ {
public class MoveElements: RunnableOnFinish public class MoveElements: OperationWithFinishHandler
{ {
private readonly List<IStructureItem> _elementsToMove; private readonly List<IStructureItem> _elementsToMove;
private readonly PwGroup _targetGroup; private readonly PwGroup _targetGroup;
private readonly Activity _ctx;
private readonly IKp2aApp _app; private readonly IKp2aApp _app;
public MoveElements(List<IStructureItem> elementsToMove, PwGroup targetGroup, Activity ctx, IKp2aApp app, OnFinish finish) : base(ctx, finish) public MoveElements(List<IStructureItem> elementsToMove, PwGroup targetGroup,IKp2aApp app, OnOperationFinishedHandler operationFinishedHandler) : base(app, operationFinishedHandler)
{ {
_elementsToMove = elementsToMove; _elementsToMove = elementsToMove;
_targetGroup = targetGroup; _targetGroup = targetGroup;
_ctx = ctx;
_app = app; _app = app;
} }
@@ -123,24 +121,24 @@ namespace keepass2android.database.edit
int indexToSave = 0; int indexToSave = 0;
bool allSavesSuccess = true; bool allSavesSuccess = true;
void ContinueSave(bool success, string message, Activity activeActivity) void ContinueSave(bool success, string message, Context activeActivity)
{ {
allSavesSuccess &= success; allSavesSuccess &= success;
indexToSave++; indexToSave++;
if (indexToSave == allDatabasesToSave.Count) if (indexToSave == allDatabasesToSave.Count)
{ {
OnFinishToRun.SetResult(allSavesSuccess); operationFinishedHandler.SetResult(allSavesSuccess);
OnFinishToRun.Run(); operationFinishedHandler.Run();
return; return;
} }
SaveDb saveDb = new SaveDb(_ctx, _app, allDatabasesToSave[indexToSave], new ActionOnFinish(activeActivity, ContinueSave), false); SaveDb saveDb = new SaveDb( _app, allDatabasesToSave[indexToSave], new ActionOnOperationFinished(_app, ContinueSave), false, null);
saveDb.SetStatusLogger(StatusLogger); saveDb.SetStatusLogger(StatusLogger);
saveDb.ShowDatabaseIocInStatus = allDatabasesToSave.Count > 1; saveDb.ShowDatabaseIocInStatus = allDatabasesToSave.Count > 1;
saveDb.Run(); saveDb.Run();
} }
SaveDb save = new SaveDb(_ctx, _app, allDatabasesToSave[0], new ActionOnFinish(ActiveActivity, ContinueSave), false); SaveDb save = new SaveDb(_app, allDatabasesToSave[0], new ActionOnOperationFinished(_app, ContinueSave), false, null);
save.SetStatusLogger(StatusLogger); save.SetStatusLogger(StatusLogger);
save.ShowDatabaseIocInStatus = allDatabasesToSave.Count > 1; save.ShowDatabaseIocInStatus = allDatabasesToSave.Count > 1;
save.Run(); save.Run();

View File

@@ -22,10 +22,16 @@ using Android.Content;
using Android.OS; using Android.OS;
using Android.Widget; using Android.Widget;
using Google.Android.Material.Dialog; using Google.Android.Material.Dialog;
using KeePassLib.Interfaces;
namespace keepass2android namespace keepass2android
{ {
public abstract class OnFinish public interface IActiveContextProvider
{
Context ActiveContext { get; }
}
public abstract class OnOperationFinishedHandler
{ {
protected bool Success; protected bool Success;
protected String Message; protected String Message;
@@ -37,63 +43,41 @@ namespace keepass2android
set; set;
} }
protected OnFinish BaseOnFinish; protected Context ActiveContext
{
get
{
return _activeContextProvider?.ActiveContext;
}
}
protected OnOperationFinishedHandler NextOnOperationFinishedHandler;
protected Handler Handler; 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; private readonly IActiveContextProvider _activeContextProvider;
public IKp2aStatusLogger StatusLogger
public ProgressDialogStatusLogger StatusLogger
{ {
get { return _statusLogger; } get { return _statusLogger; }
set { _statusLogger = value; } set { _statusLogger = value; }
} } protected OnOperationFinishedHandler(IActiveContextProvider activeContextProvider, Handler handler)
public Activity ActiveActivity
{ {
get { return _activeActivity; } _activeContextProvider = activeContextProvider;
set NextOnOperationFinishedHandler = null;
{
if (_activeActivity != null && _activeActivity != _previouslyActiveActivity)
{
_previouslyActiveActivity = _activeActivity;
}
_activeActivity = value;
if (BaseOnFinish != null)
{
BaseOnFinish.ActiveActivity = value;
}
}
}
public Activity PreviouslyActiveActivity
{
get { return _previouslyActiveActivity; }
}
protected OnFinish(Activity activeActivity, Handler handler)
{
ActiveActivity = activeActivity;
BaseOnFinish = null;
Handler = handler;
}
protected OnFinish(Activity activeActivity, OnFinish finish, Handler handler)
{
ActiveActivity = activeActivity;
BaseOnFinish = finish;
Handler = handler; Handler = handler;
} }
protected OnFinish(Activity activeActivity, OnFinish finish) protected OnOperationFinishedHandler(IActiveContextProvider activeContextProvider, OnOperationFinishedHandler operationFinishedHandler, Handler handler)
{ {
ActiveActivity = activeActivity; _activeContextProvider = activeContextProvider;
BaseOnFinish = finish; NextOnOperationFinishedHandler = operationFinishedHandler;
Handler = handler;
}
protected OnOperationFinishedHandler(IActiveContextProvider activeContextProvider, OnOperationFinishedHandler operationFinishedHandler)
{
_activeContextProvider = activeContextProvider;
NextOnOperationFinishedHandler = operationFinishedHandler;
Handler = null; Handler = null;
} }
@@ -110,14 +94,19 @@ namespace keepass2android
} }
public virtual void Run() { public virtual void Run() {
if (BaseOnFinish == null) return; if (NextOnOperationFinishedHandler == null) return;
// Pass on result on call finish // Pass on result on call finish
BaseOnFinish.SetResult(Success, Message, ImportantMessage, Exception); NextOnOperationFinishedHandler.SetResult(Success, Message, ImportantMessage, Exception);
if ( Handler != null ) { var handler = Handler ?? NextOnOperationFinishedHandler.Handler ?? null;
Handler.Post(BaseOnFinish.Run);
if (handler != null ) {
handler.Post(() =>
{
NextOnOperationFinishedHandler.Run();
});
} else { } else {
BaseOnFinish.Run(); NextOnOperationFinishedHandler.Run();
} }
} }
@@ -128,7 +117,7 @@ namespace keepass2android
public static void DisplayMessage(Context ctx, string message, bool makeDialog) public static void DisplayMessage(Context ctx, string message, bool makeDialog)
{ {
if ( !String.IsNullOrEmpty(message) ) { if ( !String.IsNullOrEmpty(message) ) {
Kp2aLog.Log("OnFinish message: " + message); Kp2aLog.Log("OnOperationFinishedHandler message: " + message);
if (makeDialog && ctx != null) if (makeDialog && ctx != null)
{ {
try try
@@ -152,3 +141,4 @@ namespace keepass2android
} }
} }

View File

@@ -0,0 +1,69 @@
/*
This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file is based on Keepassdroid, Copyright Brian Pellin.
Keepass2Android 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 2 of the License, or
(at your option) any later version.
Keepass2Android 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 Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using Android.App;
using Android.Content;
using KeePassLib.Interfaces;
namespace keepass2android
{
public abstract class OperationWithFinishHandler {
protected OnOperationFinishedHandler _operationFinishedHandler;
public IKp2aStatusLogger StatusLogger = new Kp2aNullStatusLogger(); //default: empty but not null
private IActiveContextProvider _activeContextProvider;
protected OperationWithFinishHandler(IActiveContextProvider activeContextProvider, OnOperationFinishedHandler operationFinishedHandler)
{
_activeContextProvider = activeContextProvider;
_operationFinishedHandler = operationFinishedHandler;
}
public OnOperationFinishedHandler operationFinishedHandler
{
get { return _operationFinishedHandler; }
set { _operationFinishedHandler = value; }
}
protected void Finish(bool result, String message, bool importantMessage = false, Exception exception = null) {
if ( operationFinishedHandler != null ) {
operationFinishedHandler.SetResult(result, message, importantMessage, exception);
operationFinishedHandler.Run();
}
}
protected void Finish(bool result) {
if ( operationFinishedHandler != null ) {
operationFinishedHandler.SetResult(result);
operationFinishedHandler.Run();
}
}
public void SetStatusLogger(IKp2aStatusLogger statusLogger) {
if (operationFinishedHandler != null)
{
operationFinishedHandler.StatusLogger = statusLogger;
}
StatusLogger = statusLogger;
}
public abstract void Run();
}
}

View File

@@ -1,78 +0,0 @@
/*
This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file is based on Keepassdroid, Copyright Brian Pellin.
Keepass2Android 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 2 of the License, or
(at your option) any later version.
Keepass2Android 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 Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using Android.App;
using Android.Content;
namespace keepass2android
{
public abstract class RunnableOnFinish {
protected OnFinish _onFinishToRun;
public ProgressDialogStatusLogger StatusLogger = new ProgressDialogStatusLogger(); //default: empty but not null
private Activity _activeActivity;
protected RunnableOnFinish(Activity activeActivity, OnFinish finish)
{
_activeActivity = activeActivity;
_onFinishToRun = finish;
}
public OnFinish OnFinishToRun
{
get { return _onFinishToRun; }
set { _onFinishToRun = value; }
}
public Activity ActiveActivity
{
get { return _activeActivity; }
set
{
_activeActivity = value;
if (_onFinishToRun != null)
_onFinishToRun.ActiveActivity = _activeActivity;
}
}
protected void Finish(bool result, String message, bool importantMessage = false, Exception exception = null) {
if ( OnFinishToRun != null ) {
OnFinishToRun.SetResult(result, message, importantMessage, exception);
OnFinishToRun.Run();
}
}
protected void Finish(bool result) {
if ( OnFinishToRun != null ) {
OnFinishToRun.SetResult(result);
OnFinishToRun.Run();
}
}
public void SetStatusLogger(ProgressDialogStatusLogger status) {
if (OnFinishToRun != null)
{
OnFinishToRun.StatusLogger = status;
}
StatusLogger = status;
}
public abstract void Run();
}
}

View File

@@ -30,56 +30,68 @@ using keepass2android.Io;
using Debug = System.Diagnostics.Debug; using Debug = System.Diagnostics.Debug;
using Exception = System.Exception; using Exception = System.Exception;
using KeePass.Util; using KeePass.Util;
using Thread = System.Threading.Thread;
namespace keepass2android namespace keepass2android
{ {
public class SaveDb : RunnableOnFinish { /// <summary>
/// Save the database. If the file has changed, ask the user if he wants to overwrite or sync.
/// </summary>
public class SaveDb : OperationWithFinishHandler {
private readonly IKp2aApp _app; private readonly IKp2aApp _app;
private readonly Database _db; private readonly Database _db;
private readonly bool _dontSave; private readonly bool _dontSave;
private readonly IDatabaseModificationWatcher _modificationWatcher;
private bool requiresSubsequentSync = false; //if true, we need to sync the file after saving.
public bool DoNotSetStatusLoggerMessage = false;
/// <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;
private readonly Context _ctx;
private Java.Lang.Thread _workerThread; private Java.Lang.Thread _workerThread;
public SaveDb(Activity ctx, IKp2aApp app, Database db, OnFinish finish, bool dontSave) public SaveDb(IKp2aApp app, Database db, OnOperationFinishedHandler operationFinishedHandler, bool dontSave, IDatabaseModificationWatcher modificationWatcher)
: base(ctx, finish) : base(app, operationFinishedHandler)
{ {
_db = db; _db = db;
_ctx = ctx;
_app = app; _app = app;
_dontSave = dontSave; _dontSave = dontSave;
_modificationWatcher = modificationWatcher ?? new NullDatabaseModificationWatcher();
} }
/// <summary> /// <summary>
/// Constructor for sync /// Constructor for sync
/// </summary> /// </summary>
/// <param name="ctx"></param>
/// <param name="app"></param> /// <param name="app"></param>
/// <param name="finish"></param> /// <param name="operationFinishedHandler"></param>
/// <param name="dontSave"></param> /// <param name="dontSave"></param>
/// <param name="streamForOrigFile">Stream for reading the data from the (changed) original location</param> /// <param name="streamForOrigFile">Stream for reading the data from the (changed) original location</param>
public SaveDb(Activity ctx, IKp2aApp app, OnFinish finish, Database db, bool dontSave, Stream streamForOrigFile) public SaveDb(IKp2aApp app, OnOperationFinishedHandler operationFinishedHandler, Database db, bool dontSave, Stream streamForOrigFile, IDatabaseModificationWatcher modificationWatcher = null)
: base(ctx, finish) : base(app, operationFinishedHandler)
{ {
_modificationWatcher = modificationWatcher ?? new NullDatabaseModificationWatcher();
_db = db; _db = db;
_ctx = ctx;
_app = app; _app = app;
_dontSave = dontSave; _dontSave = dontSave;
_streamForOrigFile = streamForOrigFile; _streamForOrigFile = streamForOrigFile;
SyncInBackground = _app.SyncInBackgroundPreference;
} }
public SaveDb(Activity ctx, IKp2aApp app, Database db, OnFinish finish) public SaveDb(IKp2aApp app, Database db, OnOperationFinishedHandler operationFinishedHandler, IDatabaseModificationWatcher modificationWatcher = null)
: base(ctx, finish) : base(app, operationFinishedHandler)
{ {
_ctx = ctx;
_modificationWatcher = modificationWatcher ?? new NullDatabaseModificationWatcher();
_app = app; _app = app;
_db = db; _db = db;
_dontSave = false; _dontSave = false;
SyncInBackground = _app.SyncInBackgroundPreference;
} }
public bool ShowDatabaseIocInStatus { get; set; } public bool ShowDatabaseIocInStatus { get; set; }
@@ -103,29 +115,42 @@ namespace keepass2android
if (ShowDatabaseIocInStatus) if (ShowDatabaseIocInStatus)
message += " (" + _app.GetFileStorage(_db.Ioc).GetDisplayName(_db.Ioc) + ")"; message += " (" + _app.GetFileStorage(_db.Ioc).GetDisplayName(_db.Ioc) + ")";
if (!DoNotSetStatusLoggerMessage)
{
StatusLogger.UpdateMessage(message); StatusLogger.UpdateMessage(message);
}
IOConnectionInfo ioc = _db.Ioc; IOConnectionInfo ioc = _db.Ioc;
IFileStorage fileStorage = _app.GetFileStorage(ioc); IFileStorage fileStorage = _app.GetFileStorage(ioc);
if (SyncInBackground && fileStorage is IOfflineSwitchable offlineSwitchable)
{
offlineSwitchable.IsOffline = true;
//no warning. We'll trigger a sync later.
offlineSwitchable.TriggerWarningWhenFallingBackToCache = false;
requiresSubsequentSync = true;
}
if (_streamForOrigFile == null) if (_streamForOrigFile == null)
{ {
if ((!_app.GetBooleanPreference(PreferenceKey.CheckForFileChangesOnSave)) if ((!_app.GetBooleanPreference(PreferenceKey.CheckForFileChangesOnSave))
|| (_db.KpDatabase.HashOfFileOnDisk == null)) //first time saving || (_db.KpDatabase.HashOfFileOnDisk == null)) //first time saving
{ {
PerformSaveWithoutCheck(fileStorage, ioc); PerformSaveWithoutCheck(fileStorage, ioc);
Finish(true); FinishWithSuccess();
return; return;
} }
} }
bool hasStreamForOrigFile = (_streamForOrigFile != null); bool hasStreamForOrigFile = (_streamForOrigFile != null);
bool hasChangeFast = hasStreamForOrigFile || bool hasChangeFast = hasStreamForOrigFile ||
fileStorage.CheckForFileChangeFast(ioc, _db.LastFileVersion); //first try to use the fast change detection; fileStorage.CheckForFileChangeFast(ioc, _db.LastFileVersion); //first try to use the fast change detection;
bool hasHashChanged = hasChangeFast || bool hasHashChanged = !requiresSubsequentSync && (
hasChangeFast ||
(FileHashChanged(ioc, _db.KpDatabase.HashOfFileOnDisk) == (FileHashChanged(ioc, _db.KpDatabase.HashOfFileOnDisk) ==
FileHashChange.Changed); //if that fails, hash the file and compare: FileHashChange.Changed)); //if that fails, hash the file and compare:
if (hasHashChanged) if (hasHashChanged)
{ {
@@ -158,15 +183,14 @@ namespace keepass2android
RunInWorkerThread(() => RunInWorkerThread(() =>
{ {
PerformSaveWithoutCheck(fileStorage, ioc); PerformSaveWithoutCheck(fileStorage, ioc);
Finish(true); FinishWithSuccess();
}); });
}, },
//cancel //cancel
(sender, args) => (sender, args) =>
{ {
RunInWorkerThread(() => Finish(false)); RunInWorkerThread(() => Finish(false));
}, }
_ctx
); );
} }
@@ -174,7 +198,7 @@ namespace keepass2android
else else
{ {
PerformSaveWithoutCheck(fileStorage, ioc); PerformSaveWithoutCheck(fileStorage, ioc);
Finish(true); FinishWithSuccess();
} }
} }
@@ -194,21 +218,66 @@ namespace keepass2android
} }
else else
{ {
Finish(true); FinishWithSuccess();
} }
} }
public bool SyncInBackground { get; set; }
private void FinishWithSuccess()
{
if (requiresSubsequentSync)
{
var syncTask = new SynchronizeCachedDatabase(_app, _db, new ActionOnOperationFinished(_app,
(success, message, context) =>
{
if (!System.String.IsNullOrEmpty(message))
_app.ShowMessage(context, message, success ? MessageSeverity.Info : MessageSeverity.Error);
}), new BackgroundDatabaseModificationLocker(_app)
);
OperationRunner.Instance.Run(_app, syncTask);
}
Finish(true);
}
private void MergeAndFinish(IFileStorage fileStorage, IOConnectionInfo ioc) private void MergeAndFinish(IFileStorage fileStorage, IOConnectionInfo ioc)
{ {
//note: when synced, the file might be downloaded once again from the server. Caching the data //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 //in the hashing function would solve this but increases complexity. I currently assume the files are
//small. //small.
MergeIn(fileStorage, ioc);
PerformSaveWithoutCheck(fileStorage, ioc); try
_db.UpdateGlobals(); {
Finish(true); _modificationWatcher.BeforeModifyDatabases();
} }
catch (Java.Lang.InterruptedException)
{
// leave without Finish()
return;
}
try
{
MergeIn(fileStorage, ioc);
}
finally
{
_modificationWatcher.AfterModifyDatabases();
}
PerformSaveWithoutCheck(fileStorage, ioc);
new Handler(Looper.MainLooper).Post(() =>
{
_db.UpdateGlobals();
});
FinishWithSuccess();
}
private void RunInWorkerThread(Action runHandler) private void RunInWorkerThread(Action runHandler)
{ {
@@ -282,7 +351,7 @@ namespace keepass2android
private void PerformSaveWithoutCheck(IFileStorage fileStorage, IOConnectionInfo ioc) private void PerformSaveWithoutCheck(IFileStorage fileStorage, IOConnectionInfo ioc)
{ {
StatusLogger.UpdateSubMessage(""); StatusLogger.UpdateSubMessage("");
_db.SaveData(); _db.SaveData(fileStorage);
_db.LastFileVersion = fileStorage.GetCurrentFileVersionFast(ioc); _db.LastFileVersion = fileStorage.GetCurrentFileVersionFast(ioc);
} }

View File

@@ -22,26 +22,24 @@ using KeePassLib.Keys;
namespace keepass2android namespace keepass2android
{ {
public class SetPassword : RunnableOnFinish { public class SetPassword : OperationWithFinishHandler {
private readonly String _password; private readonly String _password;
private readonly String _keyfile; private readonly String _keyfile;
private readonly IKp2aApp _app; private readonly IKp2aApp _app;
private readonly bool _dontSave; private readonly bool _dontSave;
private readonly Activity _ctx;
public SetPassword(Activity ctx, IKp2aApp app, String password, String keyfile, OnFinish finish): base(ctx, finish) { public SetPassword(IKp2aApp app, String password, String keyfile, OnOperationFinishedHandler operationFinishedHandler): base(app, operationFinishedHandler) {
_ctx = ctx;
_app = app; _app = app;
_password = password; _password = password;
_keyfile = keyfile; _keyfile = keyfile;
_dontSave = false; _dontSave = false;
} }
public SetPassword(Activity ctx, IKp2aApp app, String password, String keyfile, OnFinish finish, bool dontSave) public SetPassword(IKp2aApp app, String password, String keyfile, OnOperationFinishedHandler operationFinishedHandler, bool dontSave)
: base(ctx, finish) : base(app, operationFinishedHandler)
{ {
_ctx = ctx;
_app = app; _app = app;
_password = password; _password = password;
_keyfile = keyfile; _keyfile = keyfile;
@@ -73,18 +71,18 @@ namespace keepass2android
pm.MasterKey = newKey; pm.MasterKey = newKey;
// Save Database // Save Database
_onFinishToRun = new AfterSave(ActiveActivity, previousKey, previousMasterKeyChanged, pm, OnFinishToRun); _operationFinishedHandler = new AfterSave(_app, previousKey, previousMasterKeyChanged, pm, operationFinishedHandler);
SaveDb save = new SaveDb(_ctx, _app, _app.CurrentDb, OnFinishToRun, _dontSave); SaveDb save = new SaveDb(_app, _app.CurrentDb, operationFinishedHandler, _dontSave, null);
save.SetStatusLogger(StatusLogger); save.SetStatusLogger(StatusLogger);
save.Run(); save.Run();
} }
private class AfterSave : OnFinish { private class AfterSave : OnOperationFinishedHandler {
private readonly CompositeKey _backup; private readonly CompositeKey _backup;
private readonly DateTime _previousKeyChanged; private readonly DateTime _previousKeyChanged;
private readonly PwDatabase _db; private readonly PwDatabase _db;
public AfterSave(Activity activity, CompositeKey backup, DateTime previousKeyChanged, PwDatabase db, OnFinish finish): base(activity, finish) { public AfterSave(IActiveContextProvider activeContextProvider, CompositeKey backup, DateTime previousKeyChanged, PwDatabase db, OnOperationFinishedHandler operationFinishedHandler): base(activeContextProvider, operationFinishedHandler) {
_previousKeyChanged = previousKeyChanged; _previousKeyChanged = previousKeyChanged;
_backup = backup; _backup = backup;
_db = db; _db = db;

View File

@@ -22,31 +22,29 @@ using KeePassLib;
namespace keepass2android namespace keepass2android
{ {
public class UpdateEntry : RunnableOnFinish { public class UpdateEntry : OperationWithFinishHandler {
private readonly IKp2aApp _app; private readonly IKp2aApp _app;
private readonly Activity _ctx;
public UpdateEntry(Activity ctx, IKp2aApp app, PwEntry oldE, PwEntry newE, OnFinish finish):base(ctx, finish) { public UpdateEntry(IKp2aApp app, PwEntry oldE, PwEntry newE, OnOperationFinishedHandler operationFinishedHandler):base(app, operationFinishedHandler) {
_ctx = ctx;
_app = app; _app = app;
_onFinishToRun = new AfterUpdate(ctx, oldE, newE, app, finish); _operationFinishedHandler = new AfterUpdate( oldE, newE, app, operationFinishedHandler);
} }
public override void Run() { public override void Run() {
// Commit to disk // Commit to disk
SaveDb save = new SaveDb(_ctx, _app, _app.CurrentDb, OnFinishToRun); SaveDb save = new SaveDb(_app, _app.CurrentDb, operationFinishedHandler);
save.SetStatusLogger(StatusLogger); save.SetStatusLogger(StatusLogger);
save.Run(); save.Run();
} }
private class AfterUpdate : OnFinish { private class AfterUpdate : OnOperationFinishedHandler {
private readonly PwEntry _backup; private readonly PwEntry _backup;
private readonly PwEntry _updatedEntry; private readonly PwEntry _updatedEntry;
private readonly IKp2aApp _app; private readonly IKp2aApp _app;
public AfterUpdate(Activity activity, PwEntry backup, PwEntry updatedEntry, IKp2aApp app, OnFinish finish):base(activity, finish) { public AfterUpdate(PwEntry backup, PwEntry updatedEntry, IKp2aApp app, OnOperationFinishedHandler operationFinishedHandler):base(app, operationFinishedHandler) {
_backup = backup; _backup = backup;
_updatedEntry = updatedEntry; _updatedEntry = updatedEntry;
_app = app; _app = app;

View File

@@ -228,9 +228,9 @@ namespace keepass2android
newEntry.SetUuid(new PwUuid(true), true); // Create new UUID newEntry.SetUuid(new PwUuid(true), true); // Create new UUID
string strTitle = newEntry.Strings.ReadSafe(PwDefs.TitleField); string strTitle = newEntry.Strings.ReadSafe(PwDefs.TitleField);
newEntry.Strings.Set(PwDefs.TitleField, new ProtectedString(false, strTitle + " (" + Android.OS.Build.Model + ")")); newEntry.Strings.Set(PwDefs.TitleField, new ProtectedString(false, strTitle + " (" + Android.OS.Build.Model + ")"));
var addTask = new AddEntry(this, App.Kp2a.CurrentDb, App.Kp2a, newEntry,item.Entry.ParentGroup,new ActionOnFinish(this, (success, message, activity) => ((ConfigureChildDatabasesActivity)activity).Update())); var addTask = new AddEntry( App.Kp2a.CurrentDb, App.Kp2a, newEntry,item.Entry.ParentGroup,new ActionInContextInstanceOnOperationFinished(ContextInstanceId, App.Kp2a, (success, message, context) => (context as ConfigureChildDatabasesActivity)?.Update()));
ProgressTask pt = new ProgressTask(App.Kp2a, this, addTask); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, addTask);
pt.Run(); pt.Run();
} }
@@ -260,9 +260,9 @@ namespace keepass2android
private void Save(AutoExecItem item) private void Save(AutoExecItem item)
{ {
var addTask = new SaveDb(this, App.Kp2a, App.Kp2a.FindDatabaseForElement(item.Entry), new ActionOnFinish(this, (success, message, activity) => ((ConfigureChildDatabasesActivity)activity).Update())); var addTask = new SaveDb(App.Kp2a, App.Kp2a.FindDatabaseForElement(item.Entry), new ActionInContextInstanceOnOperationFinished(ContextInstanceId, App.Kp2a, (success, message, context) => (context as ConfigureChildDatabasesActivity)?.Update()));
ProgressTask pt = new ProgressTask(App.Kp2a, this, addTask); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, addTask);
pt.Run(); pt.Run();
} }
@@ -343,7 +343,7 @@ namespace keepass2android
} }
if (autoOpenGroup == null) if (autoOpenGroup == null)
{ {
AddGroup addGroupTask = AddGroup.GetInstance(this, App.Kp2a, "AutoOpen", 1, null, rootGroup, null, true); AddGroup addGroupTask = AddGroup.GetInstance(App.Kp2a, "AutoOpen", 1, null, rootGroup, null, true);
addGroupTask.Run(); addGroupTask.Run();
autoOpenGroup = addGroupTask.Group; autoOpenGroup = addGroupTask.Group;
} }
@@ -367,9 +367,9 @@ namespace keepass2android
{KeeAutoExecExt.ThisDeviceId, true} {KeeAutoExecExt.ThisDeviceId, true}
}))); })));
var addTask = new AddEntry(this, db, App.Kp2a, newEntry, autoOpenGroup, new ActionOnFinish(this, (success, message, activity) => (activity as ConfigureChildDatabasesActivity)?.Update())); var addTask = new AddEntry( db, App.Kp2a, newEntry, autoOpenGroup, new ActionInContextInstanceOnOperationFinished(ContextInstanceId, App.Kp2a, (success, message, context) => (context as ConfigureChildDatabasesActivity)?.Update()));
ProgressTask pt = new ProgressTask(App.Kp2a, this, addTask); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, addTask);
pt.Run(); pt.Run();
} }

View File

@@ -213,10 +213,9 @@ namespace keepass2android
} }
// Create the new database // Create the new database
CreateDb create = new CreateDb(App.Kp2a, this, _ioc, new LaunchGroupActivity(_ioc, this), false, newKey, makeCurrent); CreateDb create = new CreateDb(App.Kp2a, this, _ioc, new LaunchGroupActivity(_ioc, App.Kp2a, this), false, newKey, makeCurrent);
ProgressTask createTask = new ProgressTask( BlockingOperationStarter createTask = new BlockingOperationStarter(
App.Kp2a, App.Kp2a, create);
this, create);
createTask.Run(); createTask.Run();
} }
@@ -317,7 +316,7 @@ namespace keepass2android
if (resultCode == KeePass.ResultOkPasswordGenerator) if (resultCode == KeePass.ResultOkPasswordGenerator)
{ {
String generatedPassword = data.GetStringExtra("keepass2android.password.generated_password"); String generatedPassword = data.GetStringExtra(GeneratePasswordActivity.GeneratedPasswordKey);
FindViewById<TextView>(Resource.Id.entry_password).Text = generatedPassword; FindViewById<TextView>(Resource.Id.entry_password).Text = generatedPassword;
FindViewById<TextView>(Resource.Id.entry_confpassword).Text = generatedPassword; FindViewById<TextView>(Resource.Id.entry_confpassword).Text = generatedPassword;
} }
@@ -403,11 +402,11 @@ namespace keepass2android
private class LaunchGroupActivity : FileOnFinish private class LaunchGroupActivity : FileOnFinish
{ {
readonly CreateDatabaseActivity _activity;
private readonly IOConnectionInfo _ioc; private readonly IOConnectionInfo _ioc;
private readonly CreateDatabaseActivity _activity;
public LaunchGroupActivity(IOConnectionInfo ioc, CreateDatabaseActivity activity) public LaunchGroupActivity(IOConnectionInfo ioc, IKp2aApp app, CreateDatabaseActivity activity)
: base(activity, null) : base(app, null)
{ {
_activity = activity; _activity = activity;
_ioc = ioc; _ioc = ioc;
@@ -420,7 +419,7 @@ namespace keepass2android
// Update the ongoing notification // Update the ongoing notification
App.Kp2a.UpdateOngoingNotification(); App.Kp2a.UpdateOngoingNotification();
if (PreferenceManager.GetDefaultSharedPreferences(_activity).GetBoolean(_activity.GetString(Resource.String.RememberRecentFiles_key), _activity.Resources.GetBoolean(Resource.Boolean.RememberRecentFiles_default))) if (PreferenceManager.GetDefaultSharedPreferences(App.Context).GetBoolean(App.Context.GetString(Resource.String.RememberRecentFiles_key), App.Context.Resources.GetBoolean(Resource.Boolean.RememberRecentFiles_default)))
{ {
// Add to recent files // Add to recent files
FileDbHelper dbHelper = App.Kp2a.FileDbHelper; FileDbHelper dbHelper = App.Kp2a.FileDbHelper;

View File

@@ -4,12 +4,12 @@ using KeePassLib.Serialization;
namespace keepass2android namespace keepass2android
{ {
class CreateNewFilename : RunnableOnFinish class CreateNewFilename : OperationWithFinishHandler
{ {
private readonly string _filename; private readonly string _filename;
public CreateNewFilename(Activity activity, OnFinish finish, string filename) public CreateNewFilename(IKp2aApp app, OnOperationFinishedHandler operationFinishedHandler, string filename)
: base(activity,finish) : base(app,operationFinishedHandler)
{ {
_filename = filename; _filename = filename;
} }

View File

@@ -56,6 +56,7 @@ using Android.Util;
using AndroidX.Core.Content; using AndroidX.Core.Content;
using Google.Android.Material.Dialog; using Google.Android.Material.Dialog;
using keepass2android; using keepass2android;
using keepass2android.views;
namespace keepass2android namespace keepass2android
{ {
@@ -63,7 +64,7 @@ namespace keepass2android
{ {
private readonly string _binaryToSave; private readonly string _binaryToSave;
public ExportBinaryProcessManager(int requestCode, Activity activity, string key) : base(requestCode, activity) public ExportBinaryProcessManager(int requestCode, LifecycleAwareActivity activity, string key) : base(requestCode, activity)
{ {
_binaryToSave = key; _binaryToSave = key;
} }
@@ -75,13 +76,13 @@ namespace keepass2android
protected override void SaveFile(IOConnectionInfo ioc) protected override void SaveFile(IOConnectionInfo ioc)
{ {
var task = new EntryActivity.WriteBinaryTask(_activity, App.Kp2a, new ActionOnFinish(_activity, (success, message, activity) => var task = new EntryActivity.WriteBinaryTask(App.Kp2a, new ActionOnOperationFinished(App.Kp2a, (success, message, context) =>
{ {
if (!success) if (!success)
App.Kp2a.ShowMessage(activity, message, MessageSeverity.Error); App.Kp2a.ShowMessage(context, message, MessageSeverity.Error);
} }
), ((EntryActivity)_activity).Entry.Binaries.Get(_binaryToSave), ioc); ), ((EntryActivity)_activity).Entry.Binaries.Get(_binaryToSave), ioc);
ProgressTask pt = new ProgressTask(App.Kp2a, _activity, task); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, task);
pt.Run(); pt.Run();
} }
@@ -89,6 +90,7 @@ namespace keepass2android
public override void OnSaveInstanceState(Bundle outState) public override void OnSaveInstanceState(Bundle outState)
{ {
outState.PutString("BinaryToSave", _binaryToSave); outState.PutString("BinaryToSave", _binaryToSave);
base.OnSaveInstanceState(outState);
} }
@@ -97,7 +99,7 @@ namespace keepass2android
[Activity (Label = "@string/app_name", ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden, [Activity (Label = "@string/app_name", ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden,
Theme = "@style/Kp2aTheme_ActionBar")] Theme = "@style/Kp2aTheme_ActionBar")]
public class EntryActivity : LockCloseActivity public class EntryActivity : LockCloseActivity, IProgressUiProvider
{ {
public const String KeyEntry = "entry"; public const String KeyEntry = "entry";
public const String KeyRefreshPos = "refresh_pos"; public const String KeyRefreshPos = "refresh_pos";
@@ -110,6 +112,45 @@ namespace keepass2android
protected override View? SnackbarAnchorView => FindViewById(Resource.Id.main_content); protected override View? SnackbarAnchorView => FindViewById(Resource.Id.main_content);
public class UpdateEntryActivityBroadcastReceiver : BroadcastReceiver
{
private readonly EntryActivity _activity;
public UpdateEntryActivityBroadcastReceiver(EntryActivity activity)
{
_activity = activity;
}
public override void OnReceive(Context? context, Intent? intent)
{
if (intent?.Action == Intents.DataUpdated)
{
_activity.OnDataUpdated();
}
}
}
private void OnDataUpdated()
{
if (Entry == null)
{
return;
}
var entryUId = Entry.Uuid;
if (!App.Kp2a.CurrentDb.EntriesById.ContainsKey(entryUId))
{
Finish();
return;
}
var newEntry = App.Kp2a.CurrentDb.EntriesById[entryUId];
if (!newEntry.EqualsEntry(Entry, PwCompareOptions.None, MemProtCmpMode.Full))
{
Recreate();
}
}
public static void Launch(Activity act, PwEntry pw, int pos, AppTask appTask, ActivityFlags? flags = null, int historyIndex=-1) public static void Launch(Activity act, PwEntry pw, int pos, AppTask appTask, ActivityFlags? flags = null, int historyIndex=-1)
{ {
Intent i = new Intent(act, typeof(EntryActivity)); Intent i = new Intent(act, typeof(EntryActivity));
@@ -481,8 +522,8 @@ namespace keepass2android
Entry.Expires = true; Entry.Expires = true;
Entry.Touch(true); Entry.Touch(true);
RequiresRefresh(); RequiresRefresh();
UpdateEntry update = new UpdateEntry(this, App.Kp2a, backupEntry, Entry, null); UpdateEntry update = new UpdateEntry(App.Kp2a, backupEntry, Entry, null);
ProgressTask pt = new ProgressTask(App.Kp2a, this, update); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, update);
pt.Run(); pt.Run();
} }
FillData(); FillData();
@@ -501,6 +542,12 @@ namespace keepass2android
//the rest of the things to do depends on the current app task: //the rest of the things to do depends on the current app task:
AppTask.CompleteOnCreateEntryActivity(this, notifyPluginsOnOpenThread); AppTask.CompleteOnCreateEntryActivity(this, notifyPluginsOnOpenThread);
_dataUpdatedIntentReceiver = new UpdateEntryActivityBroadcastReceiver(this);
IntentFilter filter = new IntentFilter();
filter.AddAction(Intents.DataUpdated);
ContextCompat.RegisterReceiver(this, _dataUpdatedIntentReceiver, filter, (int)ReceiverFlags.Exported);
} }
private void RemoveFromHistory() private void RemoveFromHistory()
@@ -525,13 +572,17 @@ namespace keepass2android
App.Kp2a.DirtyGroups.Add(parent); App.Kp2a.DirtyGroups.Add(parent);
} }
var saveTask = new SaveDb(this, App.Kp2a, App.Kp2a.FindDatabaseForElement(Entry), new ActionOnFinish(this, (success, message, activity) => var saveTask = new SaveDb( App.Kp2a, App.Kp2a.FindDatabaseForElement(Entry), new ActionInContextInstanceOnOperationFinished(ContextInstanceId, App.Kp2a, (success, message, context) =>
{
if (context is Activity activity)
{ {
activity.SetResult(KeePass.ExitRefresh); activity.SetResult(KeePass.ExitRefresh);
activity.Finish(); activity.Finish();
}
})); }));
ProgressTask pt = new ProgressTask(App.Kp2a, this, saveTask); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, saveTask);
pt.Run(); pt.Run();
} }
@@ -1078,6 +1129,8 @@ namespace keepass2android
UnregisterReceiver(_pluginActionReceiver); UnregisterReceiver(_pluginActionReceiver);
if (_pluginFieldReceiver != null) if (_pluginFieldReceiver != null)
UnregisterReceiver(_pluginFieldReceiver); UnregisterReceiver(_pluginFieldReceiver);
if (_dataUpdatedIntentReceiver != null)
UnregisterReceiver(_dataUpdatedIntentReceiver);
base.OnDestroy(); base.OnDestroy();
} }
@@ -1260,13 +1313,13 @@ namespace keepass2android
} }
public class WriteBinaryTask : RunnableOnFinish public class WriteBinaryTask : OperationWithFinishHandler
{ {
private readonly IKp2aApp _app; private readonly IKp2aApp _app;
private readonly ProtectedBinary _data; private readonly ProtectedBinary _data;
private IOConnectionInfo _targetIoc; private IOConnectionInfo _targetIoc;
public WriteBinaryTask(Activity activity, IKp2aApp app, OnFinish onFinish, ProtectedBinary data, IOConnectionInfo targetIoc) : base(activity, onFinish) public WriteBinaryTask(IKp2aApp app, OnOperationFinishedHandler onOperationFinishedHandler, ProtectedBinary data, IOConnectionInfo targetIoc) : base(app, onOperationFinishedHandler)
{ {
_app = app; _app = app;
_data = data; _data = data;
@@ -1354,6 +1407,7 @@ namespace keepass2android
} }
bool isPaused = false; bool isPaused = false;
private UpdateEntryActivityBroadcastReceiver _dataUpdatedIntentReceiver;
protected override void OnPause() protected override void OnPause()
{ {
@@ -1440,8 +1494,8 @@ namespace keepass2android
Finish(); Finish();
return true; return true;
case Resource.Id.menu_delete: case Resource.Id.menu_delete:
DeleteEntry task = new DeleteEntry(this, App.Kp2a, Entry, DeleteEntry task = new DeleteEntry(App.Kp2a, Entry,
new ActionOnFinish(this, (success, message, activity) => { if (success) { RequiresRefresh(); Finish();}})); new ActionOnOperationFinished(App.Kp2a, (success, message, context) => { if (success) { RequiresRefresh(); Finish();}}));
task.Start(); task.Start();
break; break;
case Resource.Id.menu_toggle_pass: case Resource.Id.menu_toggle_pass:
@@ -1504,16 +1558,16 @@ namespace keepass2android
//save the entry: //save the entry:
ActionOnFinish closeOrShowError = new ActionOnFinish(this, (success, message, activity) => ActionOnOperationFinished closeOrShowError = new ActionInContextInstanceOnOperationFinished(ContextInstanceId, App.Kp2a, (success, message, context) =>
{ {
OnFinish.DisplayMessage(this, message, true); OnOperationFinishedHandler.DisplayMessage(this, message, true);
finishAction((EntryActivity)activity); finishAction(context as EntryActivity);
}); });
RunnableOnFinish runnable = new UpdateEntry(this, App.Kp2a, initialEntry, newEntry, closeOrShowError); OperationWithFinishHandler runnable = new UpdateEntry(App.Kp2a, initialEntry, newEntry, closeOrShowError);
ProgressTask pt = new ProgressTask(App.Kp2a, this, runnable); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, runnable);
pt.Run(); pt.Run();
} }
@@ -1603,5 +1657,7 @@ namespace keepass2android
imageViewerIntent.PutExtra("EntryKey", key); imageViewerIntent.PutExtra("EntryKey", key);
StartActivity(imageViewerIntent); StartActivity(imageViewerIntent);
} }
public IProgressUi? ProgressUi => FindViewById<BackgroundOperationContainer>(Resource.Id.background_ops_container);
} }
} }

File diff suppressed because it is too large Load Diff

View File

@@ -62,7 +62,7 @@ namespace keepass2android
} }
/// <summary> /// <summary>
/// Holds the state of the EntrryEditActivity. This is required to be able to keep a partially modified entry in memory /// Holds the state of the EntryEditActivity. This is required to be able to keep a partially modified entry in memory
/// through the App variable. Serializing this state (especially the Entry/EntryInDatabase) can be a performance problem /// through the App variable. Serializing this state (especially the Entry/EntryInDatabase) can be a performance problem
/// when there are big attachements. /// when there are big attachements.
/// </summary> /// </summary>

View File

@@ -19,23 +19,23 @@ namespace keepass2android
{ {
private readonly FileFormatProvider _ffp; private readonly FileFormatProvider _ffp;
public ExportDbProcessManager(int requestCode, Activity activity, FileFormatProvider ffp) : base(requestCode, activity) public ExportDbProcessManager(int requestCode, LifecycleAwareActivity activity, FileFormatProvider ffp) : base(requestCode, activity)
{ {
_ffp = ffp; _ffp = ffp;
} }
protected override void SaveFile(IOConnectionInfo ioc) protected override void SaveFile(IOConnectionInfo ioc)
{ {
var exportDb = new ExportDatabaseActivity.ExportDb(_activity, App.Kp2a, new ActionOnFinish(_activity, (success, message, activity) => var exportDb = new ExportDatabaseActivity.ExportDb(App.Kp2a, new ActionInContextInstanceOnOperationFinished(_activity.ContextInstanceId, App.Kp2a, (success, message, context) =>
{ {
if (!success) if (!success)
App.Kp2a.ShowMessage(activity, message, MessageSeverity.Error); App.Kp2a.ShowMessage(context, message, MessageSeverity.Error);
else else
App.Kp2a.ShowMessage(activity, _activity.GetString(Resource.String.export_database_successful), MessageSeverity.Info); App.Kp2a.ShowMessage(context, _activity.GetString(Resource.String.export_database_successful), MessageSeverity.Info);
activity.Finish(); (context as Activity)?.Finish();
} }
), _ffp, ioc); ), _ffp, ioc);
ProgressTask pt = new ProgressTask(App.Kp2a, _activity, exportDb); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, exportDb);
pt.Run(); pt.Run();
} }
@@ -93,13 +93,13 @@ namespace keepass2android
get { return 0; } get { return 0; }
} }
public class ExportDb : RunnableOnFinish public class ExportDb : OperationWithFinishHandler
{ {
private readonly IKp2aApp _app; private readonly IKp2aApp _app;
private readonly FileFormatProvider _fileFormat; private readonly FileFormatProvider _fileFormat;
private IOConnectionInfo _targetIoc; private IOConnectionInfo _targetIoc;
public ExportDb(Activity activity, IKp2aApp app, OnFinish onFinish, FileFormatProvider fileFormat, IOConnectionInfo targetIoc) : base(activity, onFinish) public ExportDb(IKp2aApp app, OnOperationFinishedHandler onOperationFinishedHandler, FileFormatProvider fileFormat, IOConnectionInfo targetIoc) : base(app, onOperationFinishedHandler)
{ {
_app = app; _app = app;
this._fileFormat = fileFormat; this._fileFormat = fileFormat;

View File

@@ -12,9 +12,9 @@ namespace keepass2android
{ {
protected readonly int _requestCode; protected readonly int _requestCode;
protected readonly Activity _activity; protected readonly LifecycleAwareActivity _activity;
public FileSaveProcessManager(int requestCode, Activity activity) public FileSaveProcessManager(int requestCode, LifecycleAwareActivity activity)
{ {
_requestCode = requestCode; _requestCode = requestCode;
_activity = activity; _activity = activity;
@@ -103,7 +103,7 @@ namespace keepass2android
} }
else else
{ {
var task = new CreateNewFilename(_activity, new ActionOnFinish(_activity, (success, messageOrFilename, activity) => var task = new CreateNewFilename(App.Kp2a, new ActionOnOperationFinished(App.Kp2a, (success, messageOrFilename, activity) =>
{ {
if (!success) if (!success)
{ {
@@ -115,7 +115,7 @@ namespace keepass2android
}), filename); }), filename);
new ProgressTask(App.Kp2a, _activity, task).Run(); new BlockingOperationStarter(App.Kp2a, task).Run();
} }
return true; return true;

View File

@@ -667,10 +667,10 @@ namespace keepass2android
return true; return true;
} }
private void IocSelected(Activity activity, IOConnectionInfo ioc) private void IocSelected(Context context, IOConnectionInfo ioc)
{ {
if (OnOpen != null) if (OnOpen != null)
OnOpen(activity, ioc); OnOpen(context, ioc);
} }
public bool StartFileChooser(string defaultPath) public bool StartFileChooser(string defaultPath)
@@ -781,7 +781,7 @@ namespace keepass2android
} }
else else
{ {
var task = new CreateNewFilename(activity, new ActionOnFinish(activity, (success, messageOrFilename, newActivity) => var task = new CreateNewFilename(App.Kp2a, new ActionOnOperationFinished(App.Kp2a, (success, messageOrFilename, newActivity) =>
{ {
if (!success) if (!success)
{ {
@@ -793,7 +793,7 @@ namespace keepass2android
}), filename); }), filename);
new ProgressTask(App.Kp2a, activity, task).Run(); new BlockingOperationStarter(App.Kp2a, task).Run();
} }
} }

View File

@@ -46,6 +46,8 @@ namespace keepass2android
#endif #endif
{ {
public const string GeneratedPasswordKey = "keepass2android.password.generated_password";
private readonly int[] _buttonLengthButtonIds = new[] {Resource.Id.btn_length6, private readonly int[] _buttonLengthButtonIds = new[] {Resource.Id.btn_length6,
Resource.Id.btn_length8, Resource.Id.btn_length8,
Resource.Id.btn_length12, Resource.Id.btn_length12,
@@ -259,7 +261,7 @@ namespace keepass2android
EditText password = (EditText) FindViewById(Resource.Id.password_edit); EditText password = (EditText) FindViewById(Resource.Id.password_edit);
Intent intent = new Intent(); Intent intent = new Intent();
intent.PutExtra("keepass2android.password.generated_password", password.Text); intent.PutExtra(GeneratedPasswordKey, password.Text);
SetResult(KeePass.ResultOkPasswordGenerator, intent); SetResult(KeePass.ResultOkPasswordGenerator, intent);

View File

@@ -223,9 +223,9 @@ namespace keepass2android
(o, args) => (o, args) =>
{ {
//yes //yes
ProgressTask pt = new ProgressTask(App.Kp2a, this, BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a,
new AddTemplateEntries(this, App.Kp2a, new ActionOnFinish(this, new AddTemplateEntries(App.Kp2a, new ActionInContextInstanceOnOperationFinished(ContextInstanceId, App.Kp2a,
(success, message, activity) => ((GroupActivity)activity)?.StartAddEntry()))); (success, message, context) => (context as GroupActivity)?.StartAddEntry())));
pt.Run(); pt.Run();
}, },
(o, args) => (o, args) =>
@@ -235,7 +235,7 @@ namespace keepass2android
edit.Commit(); edit.Commit();
//no //no
StartAddEntry(); StartAddEntry();
},null, this); },null);
} }
else else

View File

@@ -43,11 +43,13 @@ using keepass2android;
using KeeTrayTOTP.Libraries; using KeeTrayTOTP.Libraries;
using AndroidX.AppCompat.Widget; using AndroidX.AppCompat.Widget;
using Google.Android.Material.Dialog; using Google.Android.Material.Dialog;
using keepass2android.views;
using SearchView = AndroidX.AppCompat.Widget.SearchView; using SearchView = AndroidX.AppCompat.Widget.SearchView;
using AndroidX.Core.Content;
namespace keepass2android namespace keepass2android
{ {
public abstract class GroupBaseActivity : LockCloseActivity public abstract class GroupBaseActivity : LockCloseActivity, IProgressUiProvider
{ {
public const String KeyEntry = "entry"; public const String KeyEntry = "entry";
public const String KeyMode = "mode"; public const String KeyMode = "mode";
@@ -201,19 +203,18 @@ namespace keepass2android
new PwUuid(MemUtil.HexStringToByteArray(data.Extras.GetString(GroupEditActivity.KeyCustomIconId))); new PwUuid(MemUtil.HexStringToByteArray(data.Extras.GetString(GroupEditActivity.KeyCustomIconId)));
String strGroupUuid = data.Extras.GetString(GroupEditActivity.KeyGroupUuid); String strGroupUuid = data.Extras.GetString(GroupEditActivity.KeyGroupUuid);
GroupBaseActivity act = this; GroupBaseActivity act = this;
Handler handler = new Handler(); OperationWithFinishHandler task;
RunnableOnFinish task;
if (strGroupUuid == null) if (strGroupUuid == null)
{ {
task = AddGroup.GetInstance(this, App.Kp2a, groupName, groupIconId, groupCustomIconId, Group, new RefreshTask(handler, this), false); task = AddGroup.GetInstance(App.Kp2a, groupName, groupIconId, groupCustomIconId, Group, CreateRefreshAction(), false);
} }
else else
{ {
PwUuid groupUuid = new PwUuid(MemUtil.HexStringToByteArray(strGroupUuid)); PwUuid groupUuid = new PwUuid(MemUtil.HexStringToByteArray(strGroupUuid));
task = new EditGroup(this, App.Kp2a, groupName, (PwIcon)groupIconId, groupCustomIconId, App.Kp2a.FindGroup(groupUuid), task = new EditGroup(App.Kp2a, groupName, (PwIcon)groupIconId, groupCustomIconId, App.Kp2a.FindGroup(groupUuid),
new RefreshTask(handler, this)); CreateRefreshAction());
} }
ProgressTask pt = new ProgressTask(App.Kp2a, act, task); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, task);
pt.Run(); pt.Run();
} }
@@ -274,6 +275,7 @@ namespace keepass2android
private IMenuItem searchItem; private IMenuItem searchItem;
private IMenuItem searchItemDummy; private IMenuItem searchItemDummy;
private bool isPaused; private bool isPaused;
private UpdateGroupBaseActivityBroadcastReceiver _dataUpdatedIntentReceiver;
protected override void OnResume() protected override void OnResume()
{ {
@@ -745,9 +747,10 @@ namespace keepass2android
_dataUpdatedIntentReceiver = new UpdateGroupBaseActivityBroadcastReceiver(this);
IntentFilter filter = new IntentFilter();
filter.AddAction(Intents.DataUpdated);
ContextCompat.RegisterReceiver(this, _dataUpdatedIntentReceiver, filter, (int)ReceiverFlags.Exported);
SetResult(KeePass.ExitNormal); SetResult(KeePass.ExitNormal);
@@ -924,14 +927,14 @@ namespace keepass2android
var moveElement = new MoveElements(elementsToMove.ToList(), Group, this, App.Kp2a, new ActionOnFinish(this, var moveElement = new MoveElements(elementsToMove.ToList(), Group, App.Kp2a, new ActionInContextInstanceOnOperationFinished(ContextInstanceId, App.Kp2a,
(success, message, activity) => (success, message, context) =>
{ {
((GroupBaseActivity)activity)?.StopMovingElements(); (context as GroupBaseActivity)?.StopMovingElements();
if (!String.IsNullOrEmpty(message)) if (!String.IsNullOrEmpty(message))
App.Kp2a.ShowMessage(activity, message, MessageSeverity.Error); App.Kp2a.ShowMessage(context, message, MessageSeverity.Error);
})); }));
var progressTask = new ProgressTask(App.Kp2a, this, moveElement); var progressTask = new BlockingOperationStarter(App.Kp2a, moveElement);
progressTask.Run(); progressTask.Run();
} }
@@ -1033,6 +1036,13 @@ namespace keepass2android
} }
} }
protected override void OnDestroy()
{
UnregisterReceiver(_dataUpdatedIntentReceiver);
base.OnDestroy();
}
public override bool OnCreateOptionsMenu(IMenu menu) public override bool OnCreateOptionsMenu(IMenu menu)
{ {
@@ -1212,7 +1222,7 @@ namespace keepass2android
return true; return true;
case Resource.Id.menu_sync: case Resource.Id.menu_sync:
new SyncUtil(this).SynchronizeDatabase(() => { }); new SyncUtil(this).StartSynchronizeDatabase(App.Kp2a.CurrentDb.Ioc);
return true; return true;
case Resource.Id.menu_work_offline: case Resource.Id.menu_work_offline:
@@ -1223,7 +1233,7 @@ namespace keepass2android
case Resource.Id.menu_work_online: case Resource.Id.menu_work_online:
App.Kp2a.OfflineMode = App.Kp2a.OfflineModePreference = false; App.Kp2a.OfflineMode = App.Kp2a.OfflineModePreference = false;
UpdateOfflineModeMenu(); UpdateOfflineModeMenu();
new SyncUtil(this).SynchronizeDatabase(() => { }); new SyncUtil(this).StartSynchronizeDatabase(App.Kp2a.CurrentDb.Ioc);
return true; return true;
case Resource.Id.menu_open_other_db: case Resource.Id.menu_open_other_db:
AppTask.SetActivityResult(this, KeePass.ExitLoadAnotherDb); AppTask.SetActivityResult(this, KeePass.ExitLoadAnotherDb);
@@ -1293,51 +1303,7 @@ namespace keepass2android
} }
public class RefreshTask : OnFinish
{
public RefreshTask(Handler handler, GroupBaseActivity act)
: base(act, handler)
{
}
public override void Run()
{
if (Success)
{
((GroupBaseActivity)ActiveActivity)?.RefreshIfDirty();
}
else
{
DisplayMessage(ActiveActivity);
}
}
}
public class AfterDeleteGroup : OnFinish
{
public AfterDeleteGroup(Handler handler, GroupBaseActivity act)
: base(act, handler)
{
}
public override void Run()
{
if (Success)
{
((GroupBaseActivity)ActiveActivity)?.RefreshIfDirty();
}
else
{
Handler.Post(() =>
{
App.Kp2a.ShowMessage(ActiveActivity ?? LocaleManager.LocalizedAppContext, "Unrecoverable error: " + Message, MessageSeverity.Error);
});
App.Kp2a.Lock(false);
}
}
}
public bool IsBeingMoved(PwUuid uuid) public bool IsBeingMoved(PwUuid uuid)
{ {
@@ -1408,6 +1374,79 @@ namespace keepass2android
{ {
GroupEditActivity.Launch(this, pwGroup.ParentGroup, pwGroup); GroupEditActivity.Launch(this, pwGroup.ParentGroup, pwGroup);
} }
public IProgressUi ProgressUi
{
get
{
return FindViewById<BackgroundOperationContainer>(Resource.Id.background_ops_container);
}
}
public void OnDataUpdated()
{
if (Group == null || FragmentManager.IsDestroyed)
{
return;
}
var groupId = Group.Uuid;
if (!App.Kp2a.CurrentDb.GroupsById.ContainsKey(groupId))
{
Finish();
return;
}
Group = App.Kp2a.CurrentDb.GroupsById[groupId];
var fragment = FragmentManager.FindFragmentById<GroupListFragment>(Resource.Id.list_fragment);
if (fragment == null)
{
throw new Exception("did not find fragment");
}
fragment.ListAdapter = new PwGroupListAdapter(this, Group);
SetGroupIcon();
SetGroupTitle();
ListAdapter?.NotifyDataSetChanged();
}
public OnOperationFinishedHandler CreateRefreshAction()
{
return new ActionInContextInstanceOnOperationFinished(
ContextInstanceId, App.Kp2a,
(success, message, context) =>
{
if (success)
{
RunOnUiThread(() =>
{
(context as GroupBaseActivity)?.RefreshIfDirty();
});
}
else
{
App.Kp2a.ShowMessage(context, message, MessageSeverity.Error);
}
}
);
}
}
public class UpdateGroupBaseActivityBroadcastReceiver : BroadcastReceiver
{
private readonly GroupBaseActivity _groupBaseActivity;
public UpdateGroupBaseActivityBroadcastReceiver(GroupBaseActivity groupBaseActivity)
{
_groupBaseActivity = groupBaseActivity;
}
public override void OnReceive(Context? context, Intent? intent)
{
if (intent?.Action == Intents.DataUpdated)
{
_groupBaseActivity.OnDataUpdated();
}
}
} }
public class GroupListFragment : ListFragment, AbsListView.IMultiChoiceModeListener public class GroupListFragment : ListFragment, AbsListView.IMultiChoiceModeListener
@@ -1470,12 +1509,12 @@ namespace keepass2android
{ {
return false; return false;
} }
Handler handler = new Handler();
switch (item.ItemId) switch (item.ItemId)
{ {
case Resource.Id.menu_delete: case Resource.Id.menu_delete:
DeleteMultipleItems((GroupBaseActivity)Activity, checkedItems, new GroupBaseActivity.RefreshTask(handler, ((GroupBaseActivity)Activity)), App.Kp2a); DeleteMultipleItems((GroupBaseActivity)Activity, checkedItems, ((GroupBaseActivity)Activity).CreateRefreshAction(), App.Kp2a);
break; break;
case Resource.Id.menu_move: case Resource.Id.menu_move:
var navMove = new NavigateToFolderAndLaunchMoveElementTask(App.Kp2a.CurrentDb, checkedItems.First().ParentGroup, checkedItems.Select(i => i.Uuid).ToList(), ((GroupBaseActivity)Activity).IsSearchResult); var navMove = new NavigateToFolderAndLaunchMoveElementTask(App.Kp2a.CurrentDb, checkedItems.First().ParentGroup, checkedItems.Select(i => i.Uuid).ToList(), ((GroupBaseActivity)Activity).IsSearchResult);
@@ -1483,10 +1522,10 @@ namespace keepass2android
break; break;
case Resource.Id.menu_copy: case Resource.Id.menu_copy:
var copyTask = new CopyEntry((GroupBaseActivity)Activity, App.Kp2a, (PwEntry)checkedItems.First(), var copyTask = new CopyEntry(App.Kp2a, (PwEntry)checkedItems.First(),
new GroupBaseActivity.RefreshTask(handler, ((GroupBaseActivity)Activity)), App.Kp2a.CurrentDb); ((GroupBaseActivity)Activity).CreateRefreshAction(), App.Kp2a.CurrentDb);
ProgressTask pt = new ProgressTask(App.Kp2a, Activity, copyTask); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, copyTask);
pt.Run(); pt.Run();
break; break;
@@ -1631,7 +1670,7 @@ namespace keepass2android
} }
public void DeleteMultipleItems(GroupBaseActivity activity, List<IStructureItem> checkedItems, OnFinish onFinish, Kp2aApp app) public void DeleteMultipleItems(GroupBaseActivity activity, List<IStructureItem> checkedItems, OnOperationFinishedHandler onOperationFinishedHandler, Kp2aApp app)
{ {
if (checkedItems.Any() == false) if (checkedItems.Any() == false)
return; return;
@@ -1662,30 +1701,30 @@ namespace keepass2android
} }
int dbIndex = 0; int dbIndex = 0;
Action<bool, string, Activity> action = null; Action<bool, string, Context> action = null;
action = (success, message, activeActivity) => action = (success, message, context) =>
{ {
if (success) if (success)
{ {
dbIndex++; dbIndex++;
if (dbIndex == itemsForDatabases.Count) if (dbIndex == itemsForDatabases.Count)
{ {
onFinish.SetResult(true); onOperationFinishedHandler.SetResult(true);
onFinish.Run(); onOperationFinishedHandler.Run();
return; return;
} }
new DeleteMultipleItemsFromOneDatabase(activity, itemsForDatabases[dbIndex].Key, new DeleteMultipleItemsFromOneDatabase(activity, itemsForDatabases[dbIndex].Key,
itemsForDatabases[dbIndex].Value, new ActionOnFinish(activeActivity, (b, s, activity1) => action(b, s, activity1)), app) itemsForDatabases[dbIndex].Value, new ActionOnOperationFinished(App.Kp2a, (b, s, activity1) => action(b, s, activity1)), app)
.Start(); .Start();
} }
else else
{ {
onFinish.SetResult(false, message, true, null); onOperationFinishedHandler.SetResult(false, message, true, null);
} }
}; };
new DeleteMultipleItemsFromOneDatabase(activity, itemsForDatabases[dbIndex].Key, new DeleteMultipleItemsFromOneDatabase(activity, itemsForDatabases[dbIndex].Key,
itemsForDatabases[dbIndex].Value, new ActionOnFinish(activity, (b, s, activity1) => action(b, s, activity1)), app) itemsForDatabases[dbIndex].Value, new ActionOnOperationFinished(App.Kp2a, (b, s, activity1) => action(b, s, activity1)), app)
.Start(); .Start();
} }

View File

@@ -25,7 +25,7 @@ using Android.Runtime;
namespace keepass2android namespace keepass2android
{ {
public abstract class LifecycleAwareActivity : AndroidX.AppCompat.App.AppCompatActivity public abstract class LifecycleAwareActivity : AndroidX.AppCompat.App.AppCompatActivity, IContextInstanceIdProvider
{ {
protected override void AttachBaseContext(Context baseContext) protected override void AttachBaseContext(Context baseContext)
{ {
@@ -84,12 +84,11 @@ namespace keepass2android
return baseRes; return baseRes;
} }
public Action? OnResumeListener { get; set; }
protected override void OnResume() protected override void OnResume()
{ {
base.OnResume(); base.OnResume();
OnResumeListener?.Invoke(); App.Kp2a.PerformPendingActions(_instanceId);
Kp2aLog.Log(ClassName + ".OnResume " + ID); Kp2aLog.Log(ClassName + ".OnResume " + ID);
if (App.Kp2a.CurrentDb == null) if (App.Kp2a.CurrentDb == null)
@@ -104,26 +103,38 @@ namespace keepass2android
protected override void OnStart() protected override void OnStart()
{ {
ProgressTask.SetNewActiveActivity(this); App.Kp2a.ActiveContext = this;
base.OnStart(); base.OnStart();
Kp2aLog.Log(ClassName + ".OnStart" + " " + ID); Kp2aLog.Log(ClassName + ".OnStart" + " " + ID);
} }
const string ID_KEY = "kp2a_context_instance_id";
const int InvalidId = -1;
private int _instanceId;
protected override void OnCreate(Bundle bundle) protected override void OnCreate(Bundle bundle)
{ {
base.OnCreate(bundle); base.OnCreate(bundle);
_instanceId = bundle?.GetInt(ID_KEY, InvalidId) ?? InvalidId;
if (_instanceId == InvalidId)
{
_instanceId = _nextContextInstanceId++;
}
OnCreateListener?.Invoke(bundle); OnCreateListener?.Invoke(bundle);
Kp2aLog.Log(ClassName + ".OnCreate" + " " + ID); Kp2aLog.Log(ClassName + ".OnCreate" + " " + ID + " (instance=" + _instanceId +")");
Kp2aLog.Log(ClassName + ":apptask=" + Intent.GetStringExtra("KP2A_APP_TASK_TYPE") + " " + ID); Kp2aLog.Log(ClassName + ":apptask=" + Intent.GetStringExtra("KP2A_APP_TASK_TYPE") + " " + ID);
} }
protected override void OnDestroy() protected override void OnDestroy()
{ {
base.OnDestroy(); base.OnDestroy();
Kp2aLog.Log(ClassName + ".OnDestroy" + IsFinishing.ToString() + " " + ID); Kp2aLog.Log(ClassName + ".OnDestroy " + IsFinishing.ToString() + " " + ID);
} }
protected override void OnPause() protected override void OnPause()
@@ -136,14 +147,18 @@ namespace keepass2android
{ {
base.OnStop(); base.OnStop();
Kp2aLog.Log(ClassName + ".OnStop" + " " + ID); Kp2aLog.Log(ClassName + ".OnStop" + " " + ID);
ProgressTask.RemoveActiveActivity(this);
} }
protected override void OnSaveInstanceState(Bundle outState) protected override void OnSaveInstanceState(Bundle outState)
{ {
base.OnSaveInstanceState(outState); base.OnSaveInstanceState(outState);
outState.PutInt(ID_KEY, _instanceId);
OnSaveInstanceStateListener?.Invoke(outState); OnSaveInstanceStateListener?.Invoke(outState);
} }
static int _nextContextInstanceId = 0;
public int ContextInstanceId => _instanceId;
} }
} }

View File

@@ -38,7 +38,7 @@ namespace keepass2android
protected const string NoLockCheck = "NO_LOCK_CHECK"; protected const string NoLockCheck = "NO_LOCK_CHECK";
protected IOConnectionInfo _ioc; protected IOConnectionInfo _ioc;
private BroadcastReceiver _intentReceiver; private BroadcastReceiver _lockCloseIntentReceiver;
private ActivityDesign _design; private ActivityDesign _design;
public LockCloseActivity() public LockCloseActivity()
@@ -66,11 +66,11 @@ namespace keepass2android
if (Intent.GetBooleanExtra(NoLockCheck, false)) if (Intent.GetBooleanExtra(NoLockCheck, false))
return; return;
_intentReceiver = new LockCloseActivityBroadcastReceiver(this); _lockCloseIntentReceiver = new LockCloseActivityBroadcastReceiver(this);
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
filter.AddAction(Intents.DatabaseLocked); filter.AddAction(Intents.DatabaseLocked);
filter.AddAction(Intent.ActionScreenOff); filter.AddAction(Intent.ActionScreenOff);
ContextCompat.RegisterReceiver(this, _intentReceiver, filter, (int)ReceiverFlags.Exported); ContextCompat.RegisterReceiver(this, _lockCloseIntentReceiver, filter, (int)ReceiverFlags.Exported);
} }
protected override void OnDestroy() protected override void OnDestroy()
@@ -79,7 +79,7 @@ namespace keepass2android
{ {
try try
{ {
UnregisterReceiver(_intentReceiver); UnregisterReceiver(_lockCloseIntentReceiver);
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@@ -266,6 +266,7 @@ The scheme=file is still there for old OS devices. It's also queried by apps lik
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" android:maxSdkVersion="22" /> <uses-permission android:name="android.permission.USE_CREDENTIALS" android:maxSdkVersion="22" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" android:maxSdkVersion="22" /> <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" android:maxSdkVersion="22" />
<uses-permission android:name="keepass2android.keepass2android_debug.permission.KP2aInternalFileBrowsing" /> <uses-permission android:name="keepass2android.keepass2android_debug.permission.KP2aInternalFileBrowsing" />

View File

@@ -278,6 +278,7 @@ The scheme=file is still there for old OS devices. It's also queried by apps lik
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" android:maxSdkVersion="22" /> <uses-permission android:name="android.permission.USE_CREDENTIALS" android:maxSdkVersion="22" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" android:maxSdkVersion="22" /> <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" android:maxSdkVersion="22" />
<uses-permission android:name="keepass2android.keepass2android.permission.KP2aInternalFileBrowsing" /> <uses-permission android:name="keepass2android.keepass2android.permission.KP2aInternalFileBrowsing" />

View File

@@ -257,6 +257,9 @@ The scheme=file is still there for old OS devices. It's also queried by apps lik
<uses-permission android:name="android.permission.USE_FINGERPRINT" /> <uses-permission android:name="android.permission.USE_FINGERPRINT" />
<uses-permission android:name="android.permission.USE_BIOMETRIC" /> <uses-permission android:name="android.permission.USE_BIOMETRIC" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-feature android:name="android.hardware.camera" android:required="false" /> <uses-feature android:name="android.hardware.camera" android:required="false" />

View File

@@ -222,6 +222,7 @@ namespace keepass2android
//StackBaseActivity will launch the next activity //StackBaseActivity will launch the next activity
Intent data = new Intent(); Intent data = new Intent();
data.PutExtra("ioc", IOConnectionInfo.SerializeToString(_ioConnection)); data.PutExtra("ioc", IOConnectionInfo.SerializeToString(_ioConnection));
data.PutExtra("requiresSubsequentSync", _lastLoadOperation?.RequiresSubsequentSync == true);
SetResult(Result.Ok, data); SetResult(Result.Ok, data);
@@ -1440,14 +1441,21 @@ namespace keepass2android
MakePasswordMaskedOrVisible(); MakePasswordMaskedOrVisible();
Handler handler = new Handler(); Handler handler = new Handler();
OnFinish onFinish = new AfterLoad(handler, this, _ioConnection); OnOperationFinishedHandler onOperationFinishedHandler = new AfterLoad(handler, this, _ioConnection);
LoadDb task = (KeyProviderTypes.Contains(KeyProviders.Otp)) LoadDb loadOperation = (KeyProviderTypes.Contains(KeyProviders.Otp))
? new SaveOtpAuxFileAndLoadDb(App.Kp2a, _ioConnection, _loadDbFileTask, compositeKey, GetKeyProviderString(), ? new SaveOtpAuxFileAndLoadDb(App.Kp2a, _ioConnection, _loadDbFileTask, compositeKey, GetKeyProviderString(),
onFinish, this, true, _makeCurrent) onOperationFinishedHandler, this, true, _makeCurrent)
: new LoadDb(this, App.Kp2a, _ioConnection, _loadDbFileTask, compositeKey, GetKeyProviderString(), onFinish,true, _makeCurrent); : new LoadDb(App.Kp2a, _ioConnection, _loadDbFileTask, compositeKey, GetKeyProviderString(), onOperationFinishedHandler,true, _makeCurrent);
_loadDbFileTask = null; // prevent accidental re-use _loadDbFileTask = null; // prevent accidental re-use
new ProgressTask(App.Kp2a, this, task).Run(); _lastLoadOperation = loadOperation;
//Don't use BlockingOperationStarter as that would cancel running operations.
//This is bad when used with AutoOpen: we might get here when one database has loaded and is now synced in the background.
//We don't want to cancel this.
OperationRunner.Instance.Run(App.Kp2a, loadOperation, true);
} }
catch (Exception e) catch (Exception e)
{ {
@@ -1572,6 +1580,8 @@ namespace keepass2android
} }
private bool hasRequestedKeyboardActivation = false; private bool hasRequestedKeyboardActivation = false;
private LoadDb _lastLoadOperation;
protected override void OnStart() protected override void OnStart()
{ {
base.OnStart(); base.OnStart();
@@ -1753,17 +1763,10 @@ namespace keepass2android
cbOfflineMode.Checked = cbOfflineMode.Checked =
App.Kp2a App.Kp2a
.OfflineModePreference; //this won't overwrite new user settings because every change is directly saved in settings .OfflineModePreference; //this won't overwrite new user settings because every change is directly saved in settings
LinearLayout offlineModeContainer = FindViewById<LinearLayout>(Resource.Id.work_offline_container);
var cachingFileStorage = App.Kp2a.GetFileStorage(_ioConnection) as CachingFileStorage; CheckBox cbSyncInBackground = (CheckBox)FindViewById(Resource.Id.sync_in_background)!;
if ((cachingFileStorage != null) && cachingFileStorage.IsCached(_ioConnection)) cbSyncInBackground.Checked = App.Kp2a.SyncInBackgroundPreference;
{ UpdateInternalCacheCheckboxesVisibility();
offlineModeContainer.Visibility = ViewStates.Visible;
}
else
{
offlineModeContainer.Visibility = ViewStates.Gone;
App.Kp2a.OfflineMode = false;
}
@@ -1891,12 +1894,13 @@ namespace keepass2android
// to retry with typing the full password, but that's intended to avoid showing the password to a // to retry with typing the full password, but that's intended to avoid showing the password to a
// a potentially unauthorized user (feature request https://keepass2android.codeplex.com/workitem/274) // a potentially unauthorized user (feature request https://keepass2android.codeplex.com/workitem/274)
Handler handler = new Handler(); Handler handler = new Handler();
OnFinish onFinish = new AfterLoad(handler, this, _ioConnection); OnOperationFinishedHandler onOperationFinishedHandler = new AfterLoad(handler, this, _ioConnection);
_performingLoad = true; _performingLoad = true;
LoadDb task = new LoadDb(this, App.Kp2a, _ioConnection, _loadDbFileTask, compositeKeyForImmediateLoad, GetKeyProviderString(), LoadDb loadOperation = new LoadDb(App.Kp2a, _ioConnection, _loadDbFileTask, compositeKeyForImmediateLoad, GetKeyProviderString(),
onFinish, false, _makeCurrent); onOperationFinishedHandler, false, _makeCurrent);
_loadDbFileTask = null; // prevent accidental re-use _loadDbFileTask = null; // prevent accidental re-use
new ProgressTask(App.Kp2a, this, task).Run(); _lastLoadOperation = loadOperation;
OperationRunner.Instance.Run(App.Kp2a, loadOperation, true);
compositeKeyForImmediateLoad = null; //don't reuse or keep in memory compositeKeyForImmediateLoad = null; //don't reuse or keep in memory
} }
@@ -2040,6 +2044,34 @@ namespace keepass2android
App.Kp2a.OfflineModePreference = App.Kp2a.OfflineMode = args.IsChecked; App.Kp2a.OfflineModePreference = App.Kp2a.OfflineMode = args.IsChecked;
}; };
CheckBox cbSyncInBackground = (CheckBox)FindViewById(Resource.Id.sync_in_background);
cbSyncInBackground.CheckedChange += (sender, args) =>
{
App.Kp2a.SyncInBackgroundPreference = args.IsChecked;
UpdateInternalCacheCheckboxesVisibility();
};
}
private void UpdateInternalCacheCheckboxesVisibility()
{
LinearLayout syncInBackgroundContainer = FindViewById<LinearLayout>(Resource.Id.sync_in_background_container)!;
LinearLayout offlineModeContainer = FindViewById<LinearLayout>(Resource.Id.work_offline_container)!;
var cachingFileStorage = App.Kp2a.GetFileStorage(_ioConnection) as CachingFileStorage;
if ((cachingFileStorage != null) && cachingFileStorage.IsCached(_ioConnection))
{
syncInBackgroundContainer.Visibility = ViewStates.Visible;
offlineModeContainer.Visibility =
App.Kp2a.SyncInBackgroundPreference ? ViewStates.Gone : ViewStates.Visible;
}
else
{
syncInBackgroundContainer.Visibility = offlineModeContainer.Visibility = ViewStates.Gone;
App.Kp2a.OfflineMode = false;
}
} }
private String LoadKeyProviderStringForIoc(String filename) { private String LoadKeyProviderStringForIoc(String filename) {
@@ -2110,11 +2142,11 @@ namespace keepass2android
Finish(); Finish();
} }
private class AfterLoad : OnFinish { private class AfterLoad : OnOperationFinishedHandler {
readonly PasswordActivity _act; readonly PasswordActivity _act;
private readonly IOConnectionInfo _ioConnection; private readonly IOConnectionInfo _ioConnection;
public AfterLoad(Handler handler, PasswordActivity act, IOConnectionInfo ioConnection):base(act, handler) public AfterLoad(Handler handler, PasswordActivity act, IOConnectionInfo ioConnection):base(App.Kp2a, handler)
{ {
_act = act; _act = act;
_ioConnection = ioConnection; _ioConnection = ioConnection;
@@ -2221,7 +2253,7 @@ namespace keepass2android
if (!Success) if (!Success)
_act.InitFingerprintUnlock(); _act.InitFingerprintUnlock();
_act._lastLoadOperation = null;
_act._performingLoad = false; _act._performingLoad = false;
} }
@@ -2255,7 +2287,7 @@ namespace keepass2android
private readonly PasswordActivity _act; private readonly PasswordActivity _act;
public SaveOtpAuxFileAndLoadDb(IKp2aApp app, IOConnectionInfo ioc, Task<MemoryStream> databaseData, CompositeKey compositeKey, string keyfileOrProvider, OnFinish finish, PasswordActivity act, bool updateLastUsageTimestamp, bool makeCurrent) : base(act, app, ioc, databaseData, compositeKey, keyfileOrProvider, finish,updateLastUsageTimestamp,makeCurrent) public SaveOtpAuxFileAndLoadDb(IKp2aApp app, IOConnectionInfo ioc, Task<MemoryStream> databaseData, CompositeKey compositeKey, string keyfileOrProvider, OnOperationFinishedHandler operationFinishedHandler, PasswordActivity act, bool updateLastUsageTimestamp, bool makeCurrent) : base(app, ioc, databaseData, compositeKey, keyfileOrProvider, operationFinishedHandler,updateLastUsageTimestamp,makeCurrent)
{ {
_act = act; _act = act;
} }

View File

@@ -339,9 +339,9 @@ namespace keepass2android
if (PreferenceManager.GetDefaultSharedPreferences(this) if (PreferenceManager.GetDefaultSharedPreferences(this)
.GetBoolean(GetString(Resource.String.SyncAfterQuickUnlock_key), false)) .GetBoolean(GetString(Resource.String.SyncAfterQuickUnlock_key), false))
{ {
new SyncUtil(this).SynchronizeDatabase(Finish); new SyncUtil(this).StartSynchronizeDatabase(App.Kp2a.CurrentDb.Ioc);
} }
else
Finish(); Finish();

View File

@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/background_ops_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/md_theme_surfaceVariant"
android:orientation="vertical"
>
<com.google.android.material.progressindicator.LinearProgressIndicator
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/background_ops_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="3dp"
android:text="" />
<TextView
android:id="@+id/background_ops_submessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="3dp"
android:textSize="12sp"
android:text="" />
</LinearLayout>
<Button
android:id="@+id/cancel_background"
style="?attr/materialIconButtonStyle"
app:icon="@drawable/baseline_close_24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dip"
android:layout_margin="6dip"
android:layout_weight="0"
/>
</LinearLayout>
</LinearLayout>

View File

@@ -43,7 +43,6 @@
style="@style/EntryEditSingleLine_EditText" style="@style/EntryEditSingleLine_EditText"
android:layout_marginRight="0dip" /> android:layout_marginRight="0dip" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
>
<CheckBox <CheckBox
android:id="@+id/protection" android:id="@+id/protection"

View File

@@ -75,6 +75,11 @@ android:layout_height="wrap_content">
android:layout_marginRight="0dip" android:layout_marginRight="0dip"
android:visibility="gone" android:visibility="gone"
/> />
<CheckBox
android:id="@+id/protection"
android:layout_width="0dip"
android:layout_height="0dip"
android:visibility="gone"/>
</LinearLayout> </LinearLayout>
</RelativeLayout> </RelativeLayout>

View File

@@ -4,18 +4,35 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true"> android:fitsSystemWindows="true">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<keepass2android.views.BackgroundOperationContainer
android:visibility="gone"
android:id="@+id/background_ops_container"
android:layout_below="@id/top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ScrollView <ScrollView
android:id="@+id/entry_scroll" android:id="@+id/entry_scroll"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:fillViewport="true"
android:background="?android:attr/colorBackground" android:background="?android:attr/colorBackground"
android:scrollbarStyle="insideOverlay"> android:scrollbarStyle="insideOverlay">
<keepass2android.view.EntryContentsView <keepass2android.view.EntryContentsView
android:id="@+id/entry_contents" android:id="@+id/entry_contents"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_width="fill_parent" /> android:layout_width="fill_parent" />
</ScrollView> </ScrollView>
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton <com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/entry_edit" android:id="@+id/entry_edit"
android:layout_width="wrap_content" android:layout_width="wrap_content"

View File

@@ -355,12 +355,21 @@
android:layout_above="@id/bottom_bar" android:layout_above="@id/bottom_bar"
android:background="#b8b8b8" /> android:background="#b8b8b8" />
<keepass2android.views.BackgroundOperationContainer
android:visibility="gone"
android:id="@+id/background_ops_container"
android:layout_below="@id/top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<androidx.coordinatorlayout.widget.CoordinatorLayout <androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/main_content" android:id="@+id/main_content"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_above="@id/divider2" android:layout_above="@id/divider2"
android:layout_below="@id/top" android:layout_below="@id/background_ops_container"
android:fitsSystemWindows="true"> android:fitsSystemWindows="true">
<fragment <fragment
android:name="keepass2android.GroupListFragment" android:name="keepass2android.GroupListFragment"

View File

@@ -318,6 +318,30 @@
android:text="@string/help_quickunlock" android:text="@string/help_quickunlock"
/> />
</LinearLayout> </LinearLayout>
<LinearLayout
android:id="@+id/sync_in_background_container"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/sync_in_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/SyncOfflineCacheInBackground_title" />
<keepass2android.views.Kp2aShortHelpView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextAppearance_Help_Dense"
app:help_text="@string/SyncOfflineCacheInBackground_summary"
app:title_text="@string/SyncOfflineCacheInBackground_title"
android:text="@string/SyncOfflineCacheInBackground_summary"
/>
</LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/work_offline_container" android:id="@+id/work_offline_container"
android:orientation="horizontal" android:orientation="horizontal"
@@ -328,7 +352,7 @@
android:id="@+id/work_offline" android:id="@+id/work_offline"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/UseOfflineMode" /> android:text="@string/UseOfflineMode" />
<keepass2android.views.Kp2aShortHelpView <keepass2android.views.Kp2aShortHelpView
android:layout_width="wrap_content" android:layout_width="wrap_content"
@@ -341,6 +365,7 @@
/> />
</LinearLayout> </LinearLayout>
<Button <Button
android:id="@+id/kill_app" android:id="@+id/kill_app"
android:text="@string/kill_app_label" android:text="@string/kill_app_label"

View File

@@ -137,6 +137,7 @@
<string name="NoDonationReminder_key">NoDonationReminder</string> <string name="NoDonationReminder_key">NoDonationReminder</string>
<string name="UseOfflineCache_key">UseOfflineCache</string> <string name="UseOfflineCache_key">UseOfflineCache</string>
<string name="SyncOfflineCacheInBackground_key">SyncOfflineCacheInBackground_key</string>
<string name="CreateBackups_key">CreateBackups_key</string> <string name="CreateBackups_key">CreateBackups_key</string>
<string name="AcceptAllServerCertificates_key">AcceptAllServerCertificates</string> <string name="AcceptAllServerCertificates_key">AcceptAllServerCertificates</string>
<string name="CheckForFileChangesOnSave_key">CheckForFileChangesOnSave</string> <string name="CheckForFileChangesOnSave_key">CheckForFileChangesOnSave</string>

View File

@@ -382,6 +382,8 @@
<string name="NoDonationReminder_summary">I won\'t give you a dime or I have already donated. Don\'t ask for a donation, not even at the author\'s birthday.</string> <string name="NoDonationReminder_summary">I won\'t give you a dime or I have already donated. Don\'t ask for a donation, not even at the author\'s birthday.</string>
<string name="UseOfflineCache_title">Database caching</string> <string name="UseOfflineCache_title">Database caching</string>
<string name="UseOfflineCache_summary">Keep a copy of the database files in the app\'s cache directory. This allows to use databases even while the database file is not accessible.</string> <string name="UseOfflineCache_summary">Keep a copy of the database files in the app\'s cache directory. This allows to use databases even while the database file is not accessible.</string>
<string name="SyncOfflineCacheInBackground_summary">When saving or loading, use the internal cache. Then synchronize with remote storage in a background process.</string>
<string name="SyncOfflineCacheInBackground_title">Synchronize in background</string>
<string name="CreateBackups_title">Local backups</string> <string name="CreateBackups_title">Local backups</string>
<string name="CreateBackups_summary">Create a local backup copy after successfully loading a database.</string> <string name="CreateBackups_summary">Create a local backup copy after successfully loading a database.</string>
<string name="UpdatingBackup">Updating local backup...</string> <string name="UpdatingBackup">Updating local backup...</string>
@@ -724,6 +726,9 @@
<string name="DbQuicklockedChannel_desc">Notification about the database being locked with QuickUnlock</string> <string name="DbQuicklockedChannel_desc">Notification about the database being locked with QuickUnlock</string>
<string name="EntryChannel_name">Entry notifications</string> <string name="EntryChannel_name">Entry notifications</string>
<string name="EntryChannel_desc">Notification to simplify access to the currently selected entry.</string> <string name="EntryChannel_desc">Notification to simplify access to the currently selected entry.</string>
<string name="BackgroundSyncChannel_name">Synchronization operation</string>
<string name="BackgroundSyncChannel_desc">Notification to indicate that a synchronization is performed in the background.</string>
<string name="CloseDbAfterFailedAttempts">Close database after three failed biometric unlock attempts.</string> <string name="CloseDbAfterFailedAttempts">Close database after three failed biometric unlock attempts.</string>
<string name="WarnFingerprintInvalidated">Warning! Biometric authentication can be invalidated by Android, e.g. after adding a new fingerprint in your device settings. Make sure you always know how to unlock with your master password!</string> <string name="WarnFingerprintInvalidated">Warning! Biometric authentication can be invalidated by Android, e.g. after adding a new fingerprint in your device settings. Make sure you always know how to unlock with your master password!</string>
@@ -1253,5 +1258,6 @@
<string name="switch_keyboard_for_totp_enabled">Note: You have enabled App - Password access - Autofill-Service - Autofill for TOTP entries. This can cause this window to show when you open an entry with a TOTP.</string> <string name="switch_keyboard_for_totp_enabled">Note: You have enabled App - Password access - Autofill-Service - Autofill for TOTP entries. This can cause this window to show when you open an entry with a TOTP.</string>
<string name="switch_keyboard_inside_kp2a_enabled">Note: You have enabled App - Security - Use built-in keyboard inside Keepass2Android. This can cause this window to show when you open the app or edit an entry.</string> <string name="switch_keyboard_inside_kp2a_enabled">Note: You have enabled App - Security - Use built-in keyboard inside Keepass2Android. This can cause this window to show when you open the app or edit an entry.</string>
<string name="switch_keyboard_on_search_enabled">Note: You have enabled App - Security - Password access - Keyboard switching - Switch keyboard. This can cause this window to show when you search for an entry from the browser.</string> <string name="switch_keyboard_on_search_enabled">Note: You have enabled App - Security - Password access - Keyboard switching - Switch keyboard. This can cause this window to show when you search for an entry from the browser.</string>
<string name="user_interaction_required">User interaction required. Please open the app.</string>
<string name="failed_to_access_database">Database is currently in use and cannot be accessed.</string>
</resources> </resources>

View File

@@ -12,6 +12,16 @@
android:title="@string/UseOfflineCache_title" android:title="@string/UseOfflineCache_title"
android:key="@string/UseOfflineCache_key" /> android:key="@string/UseOfflineCache_key" />
<CheckBoxPreference
android:enabled="true"
android:persistent="true"
android:summary="@string/SyncOfflineCacheInBackground_summary"
android:defaultValue="true"
android:title="@string/SyncOfflineCacheInBackground_title"
android:key="@string/SyncOfflineCacheInBackground_key" />
<CheckBoxPreference <CheckBoxPreference
android:enabled="true" android:enabled="true"
android:persistent="true" android:persistent="true"

View File

@@ -558,6 +558,7 @@ namespace keepass2android
private OpenDatabaseAdapter _adapter; private OpenDatabaseAdapter _adapter;
private MyBroadcastReceiver _intentReceiver; private MyBroadcastReceiver _intentReceiver;
private bool _isForeground; private bool _isForeground;
private readonly List<IOConnectionInfo> _pendingBackgroundSyncs = new List<IOConnectionInfo>();
public override void OnBackPressed() public override void OnBackPressed()
{ {
@@ -598,11 +599,36 @@ namespace keepass2android
string iocString = data?.GetStringExtra("ioc"); string iocString = data?.GetStringExtra("ioc");
IOConnectionInfo ioc = IOConnectionInfo.UnserializeFromString(iocString); IOConnectionInfo ioc = IOConnectionInfo.UnserializeFromString(iocString);
//we first store the required sync operation and delay its execution until we loaded all AutoOpen entries (from local file)
//if required
bool requiresSubsequentSync = data?.GetBooleanExtra("requiresSubsequentSync", false) ?? false;
if (requiresSubsequentSync)
{
_pendingBackgroundSyncs.Add(ioc);
}
if (App.Kp2a.TrySelectCurrentDb(ioc)) if (App.Kp2a.TrySelectCurrentDb(ioc))
{ {
if (OpenAutoExecEntries(App.Kp2a.CurrentDb)) return; if (OpenAutoExecEntries(App.Kp2a.CurrentDb)) return;
LaunchingOther = true; LaunchingOther = true;
AppTask.CanActivateSearchViewOnStart = true; AppTask.CanActivateSearchViewOnStart = true;
foreach (var pendingSyncIoc in _pendingBackgroundSyncs)
{
try
{
new SyncUtil(this).StartSynchronizeDatabase(pendingSyncIoc);
}
catch (Exception e)
{
App.Kp2a.ShowMessage(this, "Failed to synchronize database", MessageSeverity.Error);
Kp2aLog.LogUnexpectedError(e);
}
}
_pendingBackgroundSyncs.Clear();
AppTask.LaunchFirstGroupActivity(this); AppTask.LaunchFirstGroupActivity(this);
} }

View File

@@ -72,8 +72,8 @@ namespace keepass2android
} }
SetPassword sp = new SetPassword(_activity, App.Kp2a, pass, keyfile, new AfterSave(_activity, this, null, new Handler())); SetPassword sp = new SetPassword(App.Kp2a, pass, keyfile, new AfterSave(_activity, this, null, new Handler()));
ProgressTask pt = new ProgressTask(App.Kp2a, _activity, sp); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, sp);
pt.Run(); pt.Run();
}; };
@@ -88,21 +88,21 @@ namespace keepass2android
class AfterSave : OnFinish { class AfterSave : OnOperationFinishedHandler {
private readonly FileOnFinish _finish; private readonly FileOnFinish _operationFinishedHandler;
readonly SetPasswordDialog _dlg; readonly SetPasswordDialog _dlg;
public AfterSave(Activity activity, SetPasswordDialog dlg, FileOnFinish finish, Handler handler): base(activity, finish, handler) { public AfterSave(Activity activity, SetPasswordDialog dlg, FileOnFinish operationFinishedHandler, Handler handler): base(App.Kp2a, operationFinishedHandler, handler) {
_finish = finish; _operationFinishedHandler = operationFinishedHandler;
_dlg = dlg; _dlg = dlg;
} }
public override void Run() { public override void Run() {
if ( Success ) { if ( Success ) {
if ( _finish != null ) { if ( _operationFinishedHandler != null ) {
_finish.Filename = _dlg.Keyfile; _operationFinishedHandler.Filename = _dlg.Keyfile;
} }
FingerprintUnlockMode um; FingerprintUnlockMode um;
Enum.TryParse(PreferenceManager.GetDefaultSharedPreferences(_dlg.Context).GetString(App.Kp2a.CurrentDb.CurrentFingerprintModePrefKey, ""), out um); Enum.TryParse(PreferenceManager.GetDefaultSharedPreferences(_dlg.Context).GetString(App.Kp2a.CurrentDb.CurrentFingerprintModePrefKey, ""), out um);

View File

@@ -78,6 +78,14 @@ namespace keepass2android
protected override void OnCreate(Bundle savedInstanceState) protected override void OnCreate(Bundle savedInstanceState)
{ {
//we don't want any background thread to update/reload the database while we're in this activity.
if (!App.Kp2a.DatabasesBackgroundModificationLock.TryEnterReadLock(TimeSpan.FromSeconds(5)))
{
App.Kp2a.ShowMessage(this, GetString(Resource.String.failed_to_access_database), MessageSeverity.Error);
Finish();
return;
}
base.OnCreate(savedInstanceState); base.OnCreate(savedInstanceState);
//if user presses back to leave this activity: //if user presses back to leave this activity:
@@ -288,5 +296,12 @@ namespace keepass2android
{ {
get { return null; } get { return null; }
} }
}}
protected override void OnDestroy()
{
base.OnDestroy();
App.Kp2a.DatabasesBackgroundModificationLock.ExitReadLock();
}
}
}

View File

@@ -1,5 +1,6 @@
using System; using System;
using Android.App; using Android.App;
using Android.OS;
using Android.Widget; using Android.Widget;
using keepass2android.Io; using keepass2android.Io;
using KeePassLib.Serialization; using KeePassLib.Serialization;
@@ -8,19 +9,19 @@ namespace keepass2android
{ {
public class SyncUtil public class SyncUtil
{ {
private Activity _activity; private LifecycleAwareActivity _activity;
public SyncUtil(Activity activity) public SyncUtil(LifecycleAwareActivity activity)
{ {
_activity = activity; _activity = activity;
} }
public class SyncOtpAuxFile : RunnableOnFinish public class SyncOtpAuxFile : OperationWithFinishHandler
{ {
private readonly IOConnectionInfo _ioc; private readonly IOConnectionInfo _ioc;
public SyncOtpAuxFile(Activity activity, IOConnectionInfo ioc) public SyncOtpAuxFile(Activity activity, IOConnectionInfo ioc)
: base(activity, null) : base(App.Kp2a, null)
{ {
_ioc = ioc; _ioc = ioc;
} }
@@ -49,50 +50,45 @@ namespace keepass2android
} }
public void SynchronizeDatabase(Action runAfterSuccess) public void StartSynchronizeDatabase(IOConnectionInfo ioc)
{ {
var filestorage = App.Kp2a.GetFileStorage(App.Kp2a.CurrentDb.Ioc); var filestorage = App.Kp2a.GetFileStorage(ioc);
RunnableOnFinish task; var databaseForIoc = App.Kp2a.GetDatabase(ioc);
OnFinish onFinish = new ActionOnFinish(_activity, (success, message, activity) =>
OperationWithFinishHandler task;
OnOperationFinishedHandler onOperationFinishedHandler = new ActionInContextInstanceOnOperationFinished(_activity.ContextInstanceId, App.Kp2a, (success, message, context) =>
{
App.Kp2a.UiThreadHandler.Post(() =>
{ {
if (!String.IsNullOrEmpty(message)) if (!String.IsNullOrEmpty(message))
App.Kp2a.ShowMessage(activity, message, MessageSeverity.Error); App.Kp2a.ShowMessage(context, message, success ? MessageSeverity.Info : MessageSeverity.Error);
// Tell the adapter to refresh it's list // Tell the adapter to refresh it's list
BaseAdapter adapter = (activity as GroupBaseActivity)?.ListAdapter; BaseAdapter adapter = (context as GroupBaseActivity)?.ListAdapter;
adapter?.NotifyDataSetChanged();
if (App.Kp2a.CurrentDb?.OtpAuxFileIoc != null) adapter?.NotifyDataSetChanged();
{
var task2 = new SyncOtpAuxFile(_activity, App.Kp2a.CurrentDb.OtpAuxFileIoc);
task2.OnFinishToRun = new ActionOnFinish(_activity, (b, s, activeActivity) =>
{
runAfterSuccess();
}); });
new ProgressTask(App.Kp2a, activity, task2).Run(true);
} if (databaseForIoc?.OtpAuxFileIoc != null)
else
{ {
runAfterSuccess(); var task2 = new SyncOtpAuxFile(_activity, databaseForIoc.OtpAuxFileIoc);
OperationRunner.Instance.Run(App.Kp2a, task2);
} }
}); });
if (filestorage is CachingFileStorage) if (filestorage is CachingFileStorage)
{ {
task = new SynchronizeCachedDatabase(_activity, App.Kp2a, onFinish); task = new SynchronizeCachedDatabase(App.Kp2a, databaseForIoc, onOperationFinishedHandler, new BackgroundDatabaseModificationLocker(App.Kp2a));
} }
else else
{ {
task = new CheckDatabaseForChanges( App.Kp2a, onOperationFinishedHandler);
task = new CheckDatabaseForChanges(_activity, App.Kp2a, onFinish);
} }
OperationRunner.Instance.Run(App.Kp2a, task);
var progressTask = new ProgressTask(App.Kp2a, _activity, task);
progressTask.Run();
} }
} }

View File

@@ -45,8 +45,11 @@ namespace keepass2android.Utils
internal class ToastPresenter : IMessagePresenter internal class ToastPresenter : IMessagePresenter
{ {
public void ShowMessage(Message message) public void ShowMessage(Message message)
{
new Handler(Looper.MainLooper).Post(() =>
{ {
Toast.MakeText(App.Context, message.Text, ToastLength.Long).Show(); Toast.MakeText(App.Context, message.Text, ToastLength.Long).Show();
});
} }
public List<Message> PendingMessages => new(); public List<Message> PendingMessages => new();

View File

@@ -17,6 +17,7 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Design;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net.Security; using System.Net.Security;
@@ -46,6 +47,7 @@ using keepass2android;
using keepass2android.Utils; using keepass2android.Utils;
using KeePassLib.Interfaces; using KeePassLib.Interfaces;
using KeePassLib.Utility; using KeePassLib.Utility;
using AlertDialog = AndroidX.AppCompat.App.AlertDialog;
using Message = keepass2android.Utils.Message; using Message = keepass2android.Utils.Message;
#if !NoNet #if !NoNet
#if !EXCLUDE_JAVAFILESTORAGE #if !EXCLUDE_JAVAFILESTORAGE
@@ -56,6 +58,9 @@ using GoogleDriveAppDataFileStorage = keepass2android.Io.GoogleDriveAppDataFileS
using PCloudFileStorage = keepass2android.Io.PCloudFileStorage; using PCloudFileStorage = keepass2android.Io.PCloudFileStorage;
using static keepass2android.Util; using static keepass2android.Util;
using static Android.Provider.Telephony.MmsSms; using static Android.Provider.Telephony.MmsSms;
using AndroidX.Lifecycle;
using Java.Interop;
using keepass2android.services;
#endif #endif
#endif #endif
@@ -111,14 +116,11 @@ namespace keepass2android
#endif #endif
/// <summary> /// <summary>
/// Main implementation of the IKp2aApp interface for usage in the real app. /// Main implementation of the IKp2aApp interface for usage in the real app.
/// </summary> /// </summary>
public class Kp2aApp: IKp2aApp, ICacheSupervisor public class Kp2aApp: IKp2aApp, ICacheSupervisor
{ {
public void Lock(bool allowQuickUnlock = true, bool lockWasTriggeredByTimeout = false) public void Lock(bool allowQuickUnlock = true, bool lockWasTriggeredByTimeout = false)
{ {
if (OpenDatabases.Any()) if (OpenDatabases.Any())
@@ -148,9 +150,12 @@ namespace keepass2android
// Couldn't quick-lock, so unload database(s) instead // Couldn't quick-lock, so unload database(s) instead
_openAttempts.Clear(); _openAttempts.Clear();
_openDatabases.Clear(); _openDatabases.Clear();
_currentDatabase = null; _currentDatabase = null;
LastOpenedEntry = null; LastOpenedEntry = null;
QuickLocked = false; QuickLocked = false;
} }
} }
else else
@@ -186,10 +191,17 @@ 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,
IDatabaseModificationWatcher modificationWatcher)
{ {
var prefs = PreferenceManager.GetDefaultSharedPreferences(LocaleManager.LocalizedAppContext); var prefs = PreferenceManager.GetDefaultSharedPreferences(LocaleManager.LocalizedAppContext);
var createBackup = prefs.GetBoolean(LocaleManager.LocalizedAppContext.GetString(Resource.String.CreateBackups_key), true) var createBackup =
prefs.GetBoolean(LocaleManager.LocalizedAppContext.GetString(Resource.String.CreateBackups_key),
true)
&& !(new LocalFileStorage(this).IsLocalBackup(ioConnectionInfo)); && !(new LocalFileStorage(this).IsLocalBackup(ioConnectionInfo));
MemoryStream backupCopy = new MemoryStream(); MemoryStream backupCopy = new MemoryStream();
@@ -202,26 +214,50 @@ namespace keepass2android
memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.Seek(0, SeekOrigin.Begin);
} }
foreach (Database openDb in _openDatabases) if (!statusLogger.ContinueWork())
{ {
if (openDb.Ioc.IsSameFileAs(ioConnectionInfo)) throw new Java.Lang.InterruptedException();
{
//TODO check this earlier and simply open the database's root group
throw new Exception("Database already loaded!");
}
} }
_openAttempts.Add(ioConnectionInfo); _openAttempts.Add(ioConnectionInfo);
var newDb = new Database(new DrawableFactory(), this); var newDb = new Database(new DrawableFactory(), this);
newDb.LoadData(this, ioConnectionInfo, memoryStream, compositeKey, statusLogger, databaseFormat); newDb.LoadData(this, ioConnectionInfo, memoryStream, compositeKey, statusLogger, databaseFormat);
modificationWatcher.BeforeModifyDatabases();
try
{
if ((_currentDatabase == null) || makeCurrent)
if ((_currentDatabase == null) || makeCurrent) _currentDatabase = newDb;
bool replacedOpenDatabase = false;
for (int i = 0; i < _openDatabases.Count; i++)
{
if (_openDatabases[i].Ioc.IsSameFileAs(ioConnectionInfo))
{
if (_currentDatabase == _openDatabases[i])
{
_currentDatabase = newDb; _currentDatabase = newDb;
_openDatabases.Add(newDb); }
replacedOpenDatabase = true;
_openDatabases[i] = newDb;
break;
}
}
if (!replacedOpenDatabase)
{
_openDatabases.Add(newDb);
}
}
finally
{
modificationWatcher.AfterModifyDatabases();
}
if (createBackup) if (createBackup)
@@ -280,6 +316,7 @@ namespace keepass2android
public void CloseDatabase(Database db) public void CloseDatabase(Database db)
{ {
if (!_openDatabases.Contains(db)) if (!_openDatabases.Contains(db))
throw new Exception("Cannot close database which is not open!"); throw new Exception("Cannot close database which is not open!");
if (_openDatabases.Count == 1) if (_openDatabases.Count == 1)
@@ -287,6 +324,7 @@ namespace keepass2android
Lock(false); Lock(false);
return; return;
} }
if (LastOpenedEntry != null && db.EntriesById.ContainsKey(LastOpenedEntry.Uuid)) if (LastOpenedEntry != null && db.EntriesById.ContainsKey(LastOpenedEntry.Uuid))
{ {
LastOpenedEntry = null; LastOpenedEntry = null;
@@ -295,6 +333,7 @@ namespace keepass2android
_openDatabases.Remove(db); _openDatabases.Remove(db);
if (_currentDatabase == db) if (_currentDatabase == db)
_currentDatabase = _openDatabases.First(); _currentDatabase = _openDatabases.First();
UpdateOngoingNotification(); UpdateOngoingNotification();
//TODO broadcast event so affected activities can close/update? //TODO broadcast event so affected activities can close/update?
} }
@@ -371,12 +410,16 @@ namespace keepass2android
private readonly List<IOConnectionInfo> _openAttempts = new List<IOConnectionInfo>(); //stores which files have been attempted to open. Used to avoid that we repeatedly try to load files which failed to load. private readonly List<IOConnectionInfo> _openAttempts = new List<IOConnectionInfo>(); //stores which files have been attempted to open. Used to avoid that we repeatedly try to load files which failed to load.
private readonly List<Database> _openDatabases = new List<Database>(); private readonly List<Database> _openDatabases = new List<Database>();
private readonly List<IOConnectionInfo> _childDatabases = new List<IOConnectionInfo>(); //list of databases which were opened as child databases private readonly List<IOConnectionInfo> _childDatabases = new List<IOConnectionInfo>(); //list of databases which were opened as child databases
private Database _currentDatabase; private Database _currentDatabase;
public IEnumerable<Database> OpenDatabases public IEnumerable<Database> OpenDatabases
{ {
get { return _openDatabases; } get
{
return _openDatabases;
}
} }
internal ChallengeXCKey _currentlyWaitingXcKey; internal ChallengeXCKey _currentlyWaitingXcKey;
@@ -410,7 +453,8 @@ namespace keepass2android
DirtyGroups.Add(group); DirtyGroups.Add(group);
} }
var intent = new Intent(Intents.DataUpdated);
App.Context.SendBroadcast(intent);
} }
@@ -477,6 +521,8 @@ namespace keepass2android
// Whether the app is currently showing a dialog that requires user input, like a yesNoCancel dialog // Whether the app is currently showing a dialog that requires user input, like a yesNoCancel dialog
private bool _isShowingUserInputDialog = false; private bool _isShowingUserInputDialog = false;
private IMessagePresenter? _messagePresenter; private IMessagePresenter? _messagePresenter;
private YesNoCancelQuestion? _currentlyPendingYesNoCancelQuestion = null;
private Context? _activeContext;
private void AskForReload(Activity activity, Action<bool> actionOnResult) private void AskForReload(Activity activity, Action<bool> actionOnResult)
{ {
@@ -573,90 +619,124 @@ namespace keepass2android
return LocaleManager.LocalizedAppContext.Resources.GetDrawable((int)field.GetValue(null)); return LocaleManager.LocalizedAppContext.Resources.GetDrawable((int)field.GetValue(null));
} }
public void AskYesNoCancel(UiStringKey titleKey, UiStringKey messageKey, EventHandler<DialogClickEventArgs> yesHandler, EventHandler<DialogClickEventArgs> noHandler, EventHandler<DialogClickEventArgs> cancelHandler, Context ctx, string messageSuffix) public void AskYesNoCancel(UiStringKey titleKey, UiStringKey messageKey, EventHandler<DialogClickEventArgs> yesHandler, EventHandler<DialogClickEventArgs> noHandler, EventHandler<DialogClickEventArgs> cancelHandler, string messageSuffix)
{ {
AskYesNoCancel(titleKey, messageKey, UiStringKey.yes, UiStringKey.no, AskYesNoCancel(titleKey, messageKey, UiStringKey.yes, UiStringKey.no,
yesHandler, noHandler, cancelHandler, ctx, messageSuffix); yesHandler, noHandler, cancelHandler, messageSuffix);
} }
public void AskYesNoCancel(UiStringKey titleKey, UiStringKey messageKey, public void AskYesNoCancel(UiStringKey titleKey, UiStringKey messageKey,
UiStringKey yesString, UiStringKey noString, UiStringKey yesString, UiStringKey noString,
EventHandler<DialogClickEventArgs> yesHandler, EventHandler<DialogClickEventArgs> yesHandler,
EventHandler<DialogClickEventArgs> noHandler, EventHandler<DialogClickEventArgs> noHandler,
EventHandler<DialogClickEventArgs> cancelHandler, EventHandler<DialogClickEventArgs> cancelHandler, string messageSuffix = "")
Context ctx, string messageSuffix = "")
{ {
AskYesNoCancel(titleKey, messageKey, yesString, noString, yesHandler, noHandler, cancelHandler, null, ctx, messageSuffix); AskYesNoCancel(titleKey, messageKey, yesString, noString, yesHandler, noHandler, cancelHandler, null, messageSuffix);
} }
public void AskYesNoCancel(UiStringKey titleKey, UiStringKey messageKey, class YesNoCancelQuestion
UiStringKey yesString, UiStringKey noString,
EventHandler<DialogClickEventArgs> yesHandler,
EventHandler<DialogClickEventArgs> noHandler,
EventHandler<DialogClickEventArgs> cancelHandler,
EventHandler dismissHandler,
Context ctx, string messageSuffix = "")
{ {
Handler handler = new Handler(Looper.MainLooper); private AlertDialog? _dialog;
handler.Post(() => public UiStringKey TitleKey { get; set; }
{ public UiStringKey MessageKey { get; set; }
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(ctx); public UiStringKey YesString { get; set; }
builder.SetTitle(GetResourceString(titleKey)); public UiStringKey NoString { get; set; }
public EventHandler<DialogClickEventArgs>? YesHandler { get; set; }
public EventHandler<DialogClickEventArgs>? NoHandler { get; set; }
public EventHandler<DialogClickEventArgs>? CancelHandler { get; set; }
public EventHandler DismissHandler { get; set; }
public string MessageSuffix { get; set; }
builder.SetMessage(GetResourceString(messageKey) + (messageSuffix != "" ? " " + messageSuffix : "")); public bool TryShow(IKp2aApp app, Action onUserInputDialogClose, Action onUserInputDialogShow)
{
if (app.ActiveContext is Activity activity)
{
if (_dialog is { IsShowing: true })
{
try
{
_dialog.Dismiss();
}
catch (Exception e)
{
Kp2aLog.LogUnexpectedError(e);
}
_dialog = null;
}
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(activity);
builder.SetTitle(app.GetResourceString(TitleKey));
builder.SetMessage(app.GetResourceString(MessageKey) +
(MessageSuffix != "" ? " " + MessageSuffix : ""));
// _____handlerWithShow are wrappers around given handlers to update _isSHowingYesNoCancelDialog // _____handlerWithShow are wrappers around given handlers to update _isSHowingYesNoCancelDialog
// and to show progress dialog after yesNoCancel dialog is closed // and to show progress dialog after yesNoCancel dialog is closed
EventHandler<DialogClickEventArgs> yesHandlerWithShow = (sender, args) => EventHandler<DialogClickEventArgs> yesHandlerWithShow = (sender, args) =>
{ {
OnUserInputDialogClose(); onUserInputDialogClose();
yesHandler.Invoke(sender, args); YesHandler.Invoke(sender, args);
}; };
string yesText = GetResourceString(yesString); string yesText = app.GetResourceString(YesString);
builder.SetPositiveButton(yesText, yesHandlerWithShow); builder.SetPositiveButton(yesText, yesHandlerWithShow);
string noText = ""; string noText = "";
if (noHandler != null) if (NoHandler != null)
{ {
EventHandler<DialogClickEventArgs> noHandlerWithShow = (sender, args) => EventHandler<DialogClickEventArgs> noHandlerWithShow = (sender, args) =>
{ {
OnUserInputDialogClose(); onUserInputDialogClose();
noHandler.Invoke(sender, args); NoHandler.Invoke(sender, args);
}; };
noText = GetResourceString(noString); noText = app.GetResourceString(NoString);
builder.SetNegativeButton(noText, noHandlerWithShow); builder.SetNegativeButton(noText, noHandlerWithShow);
} }
string cancelText = ""; string cancelText = "";
if (cancelHandler != null) if (CancelHandler != null)
{ {
EventHandler<DialogClickEventArgs> cancelHandlerWithShow = (sender, args) => EventHandler<DialogClickEventArgs> cancelHandlerWithShow = (sender, args) =>
{ {
OnUserInputDialogClose(); onUserInputDialogClose();
cancelHandler.Invoke(sender, args); CancelHandler.Invoke(sender, args);
}; };
cancelText = ctx.GetString(Android.Resource.String.Cancel); cancelText = App.Context.GetString(Android.Resource.String.Cancel);
builder.SetNeutralButton(cancelText, builder.SetNeutralButton(cancelText,
cancelHandlerWithShow); cancelHandlerWithShow);
} }
var dialog = builder.Create(); _dialog = builder.Create();
if (dismissHandler != null) if (DismissHandler != null)
{ {
dialog.SetOnDismissListener(new Util.DismissListener(() => { _dialog.SetOnDismissListener(new Util.DismissListener(() =>
OnUserInputDialogClose(); {
dismissHandler(dialog, EventArgs.Empty); onUserInputDialogClose();
DismissHandler(_dialog, EventArgs.Empty);
})); }));
} }
else
{
_dialog.SetCancelable(false);
}
onUserInputDialogShow();
try
{
_dialog.Show();
}
catch (Exception e)
{
Kp2aLog.LogUnexpectedError(e);
}
OnUserInputDialogShow();
dialog.Show();
if (yesText.Length + noText.Length + cancelText.Length >= 20) if (yesText.Length + noText.Length + cancelText.Length >= 20)
{ {
try try
{ {
Button button = dialog.GetButton((int)DialogButtonType.Positive); Button button = _dialog.GetButton((int)DialogButtonType.Positive);
LinearLayout linearLayout = (LinearLayout)button.Parent; LinearLayout linearLayout = (LinearLayout)button.Parent;
linearLayout.Orientation = Orientation.Vertical; linearLayout.Orientation = Orientation.Vertical;
} }
@@ -666,14 +746,51 @@ namespace keepass2android
} }
} }
return true;
} }
); else
{
OperationRunner.Instance.StatusLogger?.UpdateSubMessage(App.Context.GetString(Resource.String.user_interaction_required));
return false;
}
}
}
public void AskYesNoCancel(UiStringKey titleKey, UiStringKey messageKey,
UiStringKey yesString, UiStringKey noString,
EventHandler<DialogClickEventArgs> yesHandler,
EventHandler<DialogClickEventArgs> noHandler,
EventHandler<DialogClickEventArgs> cancelHandler,
EventHandler dismissHandler,string messageSuffix = "")
{
if (Java.Lang.Thread.Interrupted())
{
throw new Java.Lang.InterruptedException();
}
_currentlyPendingYesNoCancelQuestion = new YesNoCancelQuestion()
{
TitleKey = titleKey,
MessageKey = messageKey,
YesString = yesString,
NoString = noString,
YesHandler = yesHandler,
NoHandler = noHandler,
CancelHandler = cancelHandler,
DismissHandler = dismissHandler,
MessageSuffix = messageSuffix
};
UiThreadHandler.Post( () =>
{
_currentlyPendingYesNoCancelQuestion.TryShow(this, OnUserInputDialogClose, OnUserInputDialogShow);
});
} }
/// <summary> /// <summary>
/// Shows all non-dismissed progress dialogs. /// Shows all non-dismissed progress dialogs.
/// If there are multiple progressDialogs active, they all will be showing. /// If there are multiple progressDialogs active, they all will be showing.
/// There probably will never be multiple dialogs at the same time because only one ProgressTask can run at a time. /// There probably will never be multiple dialogs at the same time because only one BlockingOperationStarter can run at a time.
/// Even if multiple dialogs show at the same time, it shouldn't be too much of an issue /// Even if multiple dialogs show at the same time, it shouldn't be too much of an issue
/// because they are just progress indicators. /// because they are just progress indicators.
/// </summary> /// </summary>
@@ -709,12 +826,14 @@ namespace keepass2android
private void OnUserInputDialogClose() private void OnUserInputDialogClose()
{ {
_isShowingUserInputDialog = false; _isShowingUserInputDialog = false;
_currentlyPendingYesNoCancelQuestion = null;
ShowAllActiveProgressDialogs(); ShowAllActiveProgressDialogs();
} }
public Handler UiThreadHandler public Handler UiThreadHandler
{ {
get { return new Handler(); } get { return _uiThreadHandler; }
} }
/// <summary> /// <summary>
@@ -761,8 +880,15 @@ namespace keepass2android
// Only show if asking dialog not also showing // Only show if asking dialog not also showing
if (!_app._isShowingUserInputDialog) if (!_app._isShowingUserInputDialog)
{ {
try
{
_pd.Show(); _pd.Show();
} }
catch (Exception e)
{
Kp2aLog.LogUnexpectedError(e);
}
}
} }
public void Hide() public void Hide()
@@ -773,7 +899,22 @@ namespace keepass2android
public IProgressDialog CreateProgressDialog(Context ctx) public IProgressDialog CreateProgressDialog(Context ctx)
{ {
return new RealProgressDialog(ctx, this); try
{
var pd = new RealProgressDialog(ctx, this);
pd.SetTitle(GetResourceString(UiStringKey.progress_title));
return pd;
}
catch (Exception e)
{
//may happen if the activity is (being) destroyed
Kp2aLog.Log("CreateProgressDialog failed with " + e.ToString());
return null;
}
} }
public IFileStorage GetFileStorage(IOConnectionInfo iocInfo) public IFileStorage GetFileStorage(IOConnectionInfo iocInfo)
@@ -798,9 +939,14 @@ namespace keepass2android
fileStorage = innerFileStorage; fileStorage = innerFileStorage;
} }
} }
if (fileStorage is IOfflineSwitchable) if (fileStorage is IOfflineSwitchable switchable)
{ {
((IOfflineSwitchable)fileStorage).IsOffline = App.Kp2a.OfflineMode; switchable.IsOffline = App.Kp2a.OfflineMode;
if (switchable.IsOffline)
{
//users of the file storage can set this to false, but the default is to show a warning:
switchable.TriggerWarningWhenFallingBackToCache = true;
}
} }
return fileStorage; return fileStorage;
} }
@@ -1012,6 +1158,8 @@ namespace keepass2android
FileDbHelper = new FileDbHelper(app); FileDbHelper = new FileDbHelper(app);
FileDbHelper.Open(); FileDbHelper.Open();
_uiThreadHandler = new Handler(Looper.MainLooper);
#if DEBUG #if DEBUG
foreach (UiStringKey key in Enum.GetValues(typeof(UiStringKey))) foreach (UiStringKey key in Enum.GetValues(typeof(UiStringKey)))
{ {
@@ -1151,6 +1299,31 @@ namespace keepass2android
} }
} }
public bool SyncInBackgroundPreference
{
get
{
var prefs = PreferenceManager.GetDefaultSharedPreferences(LocaleManager.LocalizedAppContext);
return prefs.GetBoolean(LocaleManager.LocalizedAppContext.GetString(Resource.String.SyncOfflineCacheInBackground_key), false);
}
set
{
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(LocaleManager.LocalizedAppContext);
ISharedPreferencesEditor edit = prefs.Edit();
edit.PutBoolean(LocaleManager.LocalizedAppContext.GetString(Resource.String.SyncOfflineCacheInBackground_key), value);
edit.Commit();
}
}
public void StartBackgroundSyncService()
{
Intent intent = new Intent(App.Context, typeof(BackgroundSyncService));
intent.SetAction(BackgroundSyncService.ActionStart);
App.Context.StartService(intent);
}
/// <summary> /// <summary>
/// true if the app is used in offline mode /// true if the app is used in offline mode
/// </summary> /// </summary>
@@ -1249,13 +1422,22 @@ namespace keepass2android
} }
public Database TryFindDatabaseForElement(IStructureItem element) public Database TryFindDatabaseForElement(IStructureItem element)
{
try
{ {
foreach (var db in OpenDatabases) foreach (var db in OpenDatabases)
{ {
//we compare UUIDs and not by reference. this is more robust and works with history items as well //we compare UUIDs and not by reference. this is more robust and works with history items as well
if (db.Elements.Any(e => e.Uuid?.Equals(element.Uuid) == true)) if (db.Elements.Any(e => e.Uuid?.Equals(element.Uuid) == true))
{
return db; return db;
} }
}
}
catch (Exception e)
{
Kp2aLog.LogUnexpectedError(e);
}
return null; return null;
} }
@@ -1329,7 +1511,89 @@ namespace keepass2android
} }
} }
public Context ActiveContext
{
get => _activeContext ?? Application.Context;
set
{
_activeContext = value;
OperationRunner.Instance.SetNewActiveContext(App.Kp2a);
_currentlyPendingYesNoCancelQuestion?.TryShow(this, OnUserInputDialogClose, OnUserInputDialogShow);
} }
}
/// <summary>
/// If the database is updated from a background operation, that operation needs to acquire a writer lock on this.
/// </summary>
/// Activities can acquire a reader lock if they want to make sure that no background operation is modifying the database while they are open.
public ReaderWriterLockSlim DatabasesBackgroundModificationLock { get; } = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
public bool CancelBackgroundOperations()
{
if (!DatabasesBackgroundModificationLock.TryEnterReadLock(TimeSpan.FromSeconds(5)))
{
return false;
}
try
{
OperationRunner.Instance.CancelAll();
}
finally
{
DatabasesBackgroundModificationLock.ExitReadLock();
}
return true;
}
private readonly Dictionary<int, List<ActionOnOperationFinished>> _pendingActionsForContextInstances = new();
private readonly object _pendingActionsForContextInstancesLock = new();
private Handler _uiThreadHandler;
public void RegisterPendingActionForContextInstance(int contextInstanceId,
ActionOnOperationFinished actionToPerformWhenContextIsResumed)
{
lock (_pendingActionsForContextInstancesLock)
{
if (!_pendingActionsForContextInstances.TryGetValue(contextInstanceId, out var actions))
{
actions = new List<ActionOnOperationFinished>();
_pendingActionsForContextInstances[contextInstanceId] = actions;
}
actions.Add(actionToPerformWhenContextIsResumed);
}
}
public void PerformPendingActions(int instanceId)
{
lock (_pendingActionsForContextInstancesLock)
{
if (_pendingActionsForContextInstances.TryGetValue(instanceId, out var actions))
{
foreach (var action in actions)
{
try
{
action.Run();
}
catch (Exception e)
{
Kp2aLog.LogUnexpectedError(e);
}
}
_pendingActionsForContextInstances.Remove(instanceId);
}
}
}
}
///Application class for Keepass2Android: Contains static Database variable to be used by all components. ///Application class for Keepass2Android: Contains static Database variable to be used by all components.
@@ -1342,7 +1606,8 @@ namespace keepass2android
[Application(Debuggable = true, Label = AppNames.AppName)] [Application(Debuggable = true, Label = AppNames.AppName)]
#endif #endif
#endif #endif
public class App : Application { public class App : Application, ILifecycleObserver
{
public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig) public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
{ {
@@ -1375,6 +1640,7 @@ namespace keepass2android
InitThaiCalendarCrashFix(); InitThaiCalendarCrashFix();
base.OnCreate(); base.OnCreate();
ProcessLifecycleOwner.Get().Lifecycle.AddObserver(this);
Kp2aLog.Log("Creating application "+PackageName+". Version=" + PackageManager.GetPackageInfo(PackageName, 0).VersionCode); Kp2aLog.Log("Creating application "+PackageName+". Version=" + PackageManager.GetPackageInfo(PackageName, 0).VersionCode);
@@ -1388,12 +1654,33 @@ namespace keepass2android
intentFilter.AddAction(Intents.LockDatabaseByTimeout); intentFilter.AddAction(Intents.LockDatabaseByTimeout);
intentFilter.AddAction(Intents.CloseDatabase); intentFilter.AddAction(Intents.CloseDatabase);
ContextCompat.RegisterReceiver(Context, broadcastReceiver, intentFilter, (int)ReceiverFlags.Exported); ContextCompat.RegisterReceiver(Context, broadcastReceiver, intentFilter, (int)ReceiverFlags.Exported);
//ZXing.Net.Mobile.Forms.Android.Platform.Init();
} }
private ApplicationBroadcastReceiver broadcastReceiver = new ApplicationBroadcastReceiver(); private ApplicationBroadcastReceiver broadcastReceiver = new ApplicationBroadcastReceiver();
[Lifecycle.Event.OnStop]
[Export]
public void OnAppBackgrounded()
{
Kp2aLog.Log("Going to background");
Kp2a.ActiveContext = null;
}
[Lifecycle.Event.OnStart]
[Export]
public void OnAppForegrounded()
{
Kp2aLog.Log("Going to foreground");
StopBackgroundSyncService();
}
private void StopBackgroundSyncService()
{
Intent stopServiceIntent = new Intent(Context, typeof(BackgroundSyncService));
stopServiceIntent.SetAction(BackgroundSyncService.ActionStop);
Context.StartService(stopServiceIntent);
}
private void CreateNotificationChannels() private void CreateNotificationChannels()
{ {

View File

@@ -37,6 +37,11 @@ namespace keepass2android
/// <summary>This intent will be broadcast once the database has been locked. Sensitive information displayed should be hidden and unloaded.</summary> /// <summary>This intent will be broadcast once the database has been locked. Sensitive information displayed should be hidden and unloaded.</summary>
public const String DatabaseLocked = "keepass2android." + AppNames.PackagePart + ".database_locked"; public const String DatabaseLocked = "keepass2android." + AppNames.PackagePart + ".database_locked";
/// <summary>
/// Signals that the loaded data was updated, e.g. by reloading during sync. All UI elements should be refreshed.
/// </summary>
public const String DataUpdated = "keepass2android." + AppNames.PackagePart + ".data_updated";
/// <summary>This intent will be broadcast once the keyboard data has been cleared</summary> /// <summary>This intent will be broadcast once the keyboard data has been cleared</summary>
public const String KeyboardCleared = "keepass2android." + AppNames.PackagePart + ".keyboard_cleared"; public const String KeyboardCleared = "keepass2android." + AppNames.PackagePart + ".keyboard_cleared";

View File

@@ -48,6 +48,14 @@ namespace keepass2android.search
{ {
protected override void OnCreate (Bundle bundle) protected override void OnCreate (Bundle bundle)
{ {
//we don't want any background thread to update/reload the database while we're in this activity. We're showing a temporary group, so background updating doesn't work well.
if (!App.Kp2a.DatabasesBackgroundModificationLock.TryEnterReadLock(TimeSpan.FromSeconds(5)))
{
App.Kp2a.ShowMessage(this, GetString(Resource.String.failed_to_access_database), MessageSeverity.Error);
Finish();
return;
}
base.OnCreate (bundle); base.OnCreate (bundle);
if ( IsFinishing ) { if ( IsFinishing ) {
@@ -59,6 +67,12 @@ namespace keepass2android.search
ProcessIntent(Intent); ProcessIntent(Intent);
} }
protected override void OnDestroy()
{
base.OnDestroy();
App.Kp2a.DatabasesBackgroundModificationLock.ExitReadLock();
}
public override bool EntriesBelongToCurrentDatabaseOnly public override bool EntriesBelongToCurrentDatabaseOnly
{ {
get { return false; } get { return false; }

View File

@@ -0,0 +1,183 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using System;
using System.Threading;
using System.Threading.Tasks;
using AndroidX.Core.App;
using Android.Content.PM;
using Microsoft.Graph.Models;
namespace keepass2android.services
{
[Service(ForegroundServiceType = ForegroundService.TypeDataSync)]
public class BackgroundSyncService : Service, IProgressUiProvider, IProgressUi
{
private const string ChannelId = "BackgroundSyncServiceChannel";
private const int NotificationId = 1;
private const string Tag = "BackgroundSyncService";
private CancellationTokenSource _cts;
private string _message;
private string _submessage;
public override void OnCreate()
{
base.OnCreate();
Log.Debug(Tag, "OnCreate");
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
if (intent.Action == ActionStop)
{
Log.Debug(Tag, "OnStartCommand: STOP");
StopForeground(StopForegroundFlags.Remove);
StopSelf();
return StartCommandResult.NotSticky;
}
Log.Debug(Tag, "OnStartCommand");
try
{
_cts = new CancellationTokenSource();
CreateNotificationChannel();
StartForeground(NotificationId, BuildNotification());
App.Kp2a.ActiveContext = this;
return StartCommandResult.Sticky;
}
catch (Exception ex)
{
Log.Error(Tag, ex.ToString());
StopForeground(StopForegroundFlags.Remove);
StopSelf();
return StartCommandResult.NotSticky;
}
}
private Notification BuildNotification()
{
Intent notificationIntent = PackageManager.GetLaunchIntentForPackage(PackageName);
if (notificationIntent == null)
{
notificationIntent = new Intent(this, typeof(FileSelectActivity));
notificationIntent.SetFlags(ActivityFlags.BroughtToFront | ActivityFlags.SingleTop |
ActivityFlags.ReorderToFront | ActivityFlags.NewTask);
}
PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, notificationIntent,
PendingIntentFlags.Immutable);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, ChannelId)
.SetSmallIcon(Resource.Drawable.ic_launcher_gray)
.SetPriority(NotificationCompat.PriorityLow)
.SetSilent(true)
.SetContentIntent(pendingIntent)
.SetProgress(100,0, true)
.SetOngoing(true);
if (!string.IsNullOrEmpty(_message))
{
builder.SetContentTitle(_message);
}
if (!string.IsNullOrEmpty(_submessage))
{
builder.SetContentText(_submessage);
}
return builder.Build();
}
private void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var channelName = GetString(Resource.String.BackgroundSyncChannel_name);
var channelDescription = GetString(Resource.String.BackgroundSyncChannel_desc);
var channelImportance = NotificationImportance.Default;
var channel = new NotificationChannel(ChannelId, channelName, channelImportance)
{
Description = channelDescription
};
channel.EnableLights(false);
channel.EnableVibration(false);
channel.SetSound(null, null);
channel.SetShowBadge(false);
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override void OnDestroy()
{
base.OnDestroy();
Log.Debug(Tag, "OnDestroy");
if (_cts != null)
{
_cts.Cancel();
_cts.Dispose();
}
}
public IProgressUi? ProgressUi
{
get { return this; }
}
public static string ActionStop => "STOP";
public static string ActionStart => "START";
public void Show()
{
}
public void Hide()
{
CloseNotification();
StopSelf();
StopForeground(StopForegroundFlags.Remove);
}
private void CloseNotification()
{
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Cancel(NotificationId);
}
public void UpdateMessage(string message)
{
_message = message;
UpdateNotification();
}
private void UpdateNotification()
{
var notification = BuildNotification();
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(NotificationId, notification);
}
public void UpdateSubMessage(string submessage)
{
_submessage = submessage;
UpdateNotification();
}
}
}

View File

@@ -117,16 +117,16 @@ namespace keepass2android
var previousUsername = db.KpDatabase.DefaultUserName; var previousUsername = db.KpDatabase.DefaultUserName;
db.KpDatabase.DefaultUserName = e.NewValue.ToString(); db.KpDatabase.DefaultUserName = e.NewValue.ToString();
SaveDb save = new SaveDb(Activity, App.Kp2a, App.Kp2a.CurrentDb, new ActionOnFinish(Activity, (success, message, activity) => SaveDb save = new SaveDb(App.Kp2a, App.Kp2a.CurrentDb, new ActionOnOperationFinished(App.Kp2a, (success, message, context) =>
{ {
if (!success) if (!success)
{ {
db.KpDatabase.DefaultUserName = previousUsername; db.KpDatabase.DefaultUserName = previousUsername;
db.KpDatabase.DefaultUserNameChanged = previousUsernameChanged; db.KpDatabase.DefaultUserNameChanged = previousUsernameChanged;
App.Kp2a.ShowMessage(activity, message, MessageSeverity.Error); App.Kp2a.ShowMessage(context, message, MessageSeverity.Error);
} }
})); }));
ProgressTask pt = new ProgressTask(App.Kp2a, Activity, save); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, save);
pt.Run(); pt.Run();
}; };
} }
@@ -144,8 +144,8 @@ namespace keepass2android
{ {
pref.PreferenceClick += (sender, args) => pref.PreferenceClick += (sender, args) =>
{ {
ProgressTask pt = new ProgressTask(App.Kp2a, Activity, BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a,
new AddTemplateEntries(Activity, App.Kp2a, new ActionOnFinish(Activity, new AddTemplateEntries(App.Kp2a, new ActionOnOperationFinished(App.Kp2a,
delegate delegate
{ {
pref.Enabled = false; pref.Enabled = false;
@@ -183,13 +183,13 @@ namespace keepass2android
String previousName = db.KpDatabase.Name; String previousName = db.KpDatabase.Name;
db.KpDatabase.Name = e.NewValue.ToString(); db.KpDatabase.Name = e.NewValue.ToString();
SaveDb save = new SaveDb(Activity, App.Kp2a, App.Kp2a.CurrentDb, new ActionOnFinish(Activity, (success, message, activity) => SaveDb save = new SaveDb(App.Kp2a, App.Kp2a.CurrentDb, new ActionOnOperationFinished(App.Kp2a, (success, message, context) =>
{ {
if (!success) if (!success)
{ {
db.KpDatabase.Name = previousName; db.KpDatabase.Name = previousName;
db.KpDatabase.NameChanged = previousNameChanged; db.KpDatabase.NameChanged = previousNameChanged;
App.Kp2a.ShowMessage(activity, message, MessageSeverity.Error); App.Kp2a.ShowMessage(context, message, MessageSeverity.Error);
} }
else else
{ {
@@ -197,7 +197,7 @@ namespace keepass2android
App.Kp2a.UpdateOngoingNotification(); App.Kp2a.UpdateOngoingNotification();
} }
})); }));
ProgressTask pt = new ProgressTask(App.Kp2a, Activity, save); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, save);
pt.Run(); pt.Run();
}; };
} }
@@ -410,18 +410,18 @@ namespace keepass2android
var previousCipher = db.KpDatabase.DataCipherUuid; var previousCipher = db.KpDatabase.DataCipherUuid;
db.KpDatabase.DataCipherUuid = new PwUuid(MemUtil.HexStringToByteArray((string)preferenceChangeEventArgs.NewValue)); db.KpDatabase.DataCipherUuid = new PwUuid(MemUtil.HexStringToByteArray((string)preferenceChangeEventArgs.NewValue));
SaveDb save = new SaveDb(Activity, App.Kp2a, App.Kp2a.CurrentDb, new ActionOnFinish(Activity, (success, message, activity) => SaveDb save = new SaveDb(App.Kp2a, App.Kp2a.CurrentDb, new ActionOnOperationFinished(App.Kp2a, (success, message, context) =>
{ {
if (!success) if (!success)
{ {
db.KpDatabase.DataCipherUuid = previousCipher; db.KpDatabase.DataCipherUuid = previousCipher;
App.Kp2a.ShowMessage(activity, message, MessageSeverity.Error); App.Kp2a.ShowMessage(context, message, MessageSeverity.Error);
return; return;
} }
preferenceChangeEventArgs.Preference.Summary = preferenceChangeEventArgs.Preference.Summary =
CipherPool.GlobalPool.GetCipher(db.KpDatabase.DataCipherUuid).DisplayName; CipherPool.GlobalPool.GetCipher(db.KpDatabase.DataCipherUuid).DisplayName;
})); }));
ProgressTask pt = new ProgressTask(App.Kp2a, Activity, save); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, save);
pt.Run(); pt.Run();
} }
@@ -1071,18 +1071,18 @@ namespace keepass2android
Kp2aLog.Log("--new kdf: " + KdfPool.Get(db.KpDatabase.KdfParameters.KdfUuid) + " " + db.KpDatabase.KdfParameters.KdfUuid.ToHexString()); Kp2aLog.Log("--new kdf: " + KdfPool.Get(db.KpDatabase.KdfParameters.KdfUuid) + " " + db.KpDatabase.KdfParameters.KdfUuid.ToHexString());
SaveDb save = new SaveDb(Activity, App.Kp2a, App.Kp2a.CurrentDb, new ActionOnFinish(Activity, (success, message, activity) => SaveDb save = new SaveDb(App.Kp2a, App.Kp2a.CurrentDb, new ActionOnOperationFinished(App.Kp2a, (success, message, context) =>
{ {
if (!success) if (!success)
{ {
db.KpDatabase.KdfParameters = previousKdfParams; db.KpDatabase.KdfParameters = previousKdfParams;
App.Kp2a.ShowMessage(activity, message, MessageSeverity.Error); App.Kp2a.ShowMessage(context, message, MessageSeverity.Error);
return; return;
} }
UpdateKdfScreen(); UpdateKdfScreen();
})); }));
ProgressTask pt = new ProgressTask(App.Kp2a, Activity, save); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, save);
pt.Run(); pt.Run();
} }

View File

@@ -13,13 +13,13 @@ namespace keepass2android
public class ExportKeyfileActivity : LockCloseActivity public class ExportKeyfileActivity : LockCloseActivity
{ {
public class ExportKeyfile : RunnableOnFinish public class ExportKeyfile : OperationWithFinishHandler
{ {
private readonly IKp2aApp _app; private readonly IKp2aApp _app;
private IOConnectionInfo _targetIoc; private IOConnectionInfo _targetIoc;
public ExportKeyfile(Activity activity, IKp2aApp app, OnFinish onFinish, IOConnectionInfo targetIoc) : base( public ExportKeyfile(IKp2aApp app, OnOperationFinishedHandler onOperationFinishedHandler, IOConnectionInfo targetIoc) : base(
activity, onFinish) App.Kp2a, onOperationFinishedHandler)
{ {
_app = app; _app = app;
_targetIoc = targetIoc; _targetIoc = targetIoc;
@@ -63,25 +63,25 @@ namespace keepass2android
public class ExportKeyfileProcessManager : FileSaveProcessManager public class ExportKeyfileProcessManager : FileSaveProcessManager
{ {
public ExportKeyfileProcessManager(int requestCode, Activity activity) : base(requestCode, activity) public ExportKeyfileProcessManager(int requestCode, LifecycleAwareActivity activity) : base(requestCode, activity)
{ {
} }
protected override void SaveFile(IOConnectionInfo ioc) protected override void SaveFile(IOConnectionInfo ioc)
{ {
var exportKeyfile = new ExportKeyfile(_activity, App.Kp2a, new ActionOnFinish(_activity, var exportKeyfile = new ExportKeyfile(App.Kp2a, new ActionInContextInstanceOnOperationFinished(_activity.ContextInstanceId, App.Kp2a,
(success, message, activity) => (success, message, context) =>
{ {
if (!success) if (!success)
App.Kp2a.ShowMessage(activity, message, MessageSeverity.Error); App.Kp2a.ShowMessage(context, message, MessageSeverity.Error);
else else
App.Kp2a.ShowMessage(activity, _activity.GetString(Resource.String.export_keyfile_successful), App.Kp2a.ShowMessage(context, _activity.GetString(Resource.String.export_keyfile_successful),
MessageSeverity.Info); MessageSeverity.Info);
activity.Finish(); (context as Activity)?.Finish();
} }
), ioc); ), ioc);
ProgressTask pt = new ProgressTask(App.Kp2a, _activity, exportKeyfile); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, exportKeyfile);
pt.Run(); pt.Run();
} }

View File

@@ -113,8 +113,8 @@ namespace keepass2android.settings
ParamValue = paramValue; ParamValue = paramValue;
Handler handler = new Handler(); Handler handler = new Handler();
SaveDb save = new SaveDb((Activity)Context, App.Kp2a, App.Kp2a.CurrentDb, new AfterSave((Activity)Context, handler, oldValue, this)); SaveDb save = new SaveDb(App.Kp2a, App.Kp2a.CurrentDb, new AfterSave(App.Kp2a, handler, oldValue, this));
ProgressTask pt = new ProgressTask(App.Kp2a, (Activity)Context, save); BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, save);
pt.Run(); pt.Run();
}); });
db.SetNegativeButton(Android.Resource.String.Cancel, ((sender, args) => { })); db.SetNegativeButton(Android.Resource.String.Cancel, ((sender, args) => { }));
@@ -129,15 +129,14 @@ namespace keepass2android.settings
public abstract ulong ParamValue { get; set; } public abstract ulong ParamValue { get; set; }
private class AfterSave : OnFinish { private class AfterSave : OnOperationFinishedHandler {
private readonly ulong _oldParamValue; private readonly ulong _oldParamValue;
private readonly Context _ctx; private readonly IKp2aApp _app;
private readonly KdfNumberDialogPreference _pref; private readonly KdfNumberDialogPreference _pref;
public AfterSave(Activity ctx, Handler handler, ulong oldParamValue, KdfNumberDialogPreference pref):base(ctx, handler) { public AfterSave(IKp2aApp app, Handler handler, ulong oldParamValue, KdfNumberDialogPreference pref):base(app, handler) {
_app = app;
_pref = pref; _pref = pref;
_ctx = ctx;
_oldParamValue = oldParamValue; _oldParamValue = oldParamValue;
} }
@@ -148,7 +147,7 @@ namespace keepass2android.settings
_pref.OnPreferenceChangeListener.OnPreferenceChange(_pref, null); _pref.OnPreferenceChangeListener.OnPreferenceChange(_pref, null);
} }
} else { } else {
DisplayMessage(_ctx); DisplayMessage(_app.ActiveContext);
_pref.ParamValue = _oldParamValue; _pref.ParamValue = _oldParamValue;
} }

View File

@@ -0,0 +1,96 @@
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
namespace keepass2android.views;
public class BackgroundOperationContainer : LinearLayout, IProgressUi
{
protected BackgroundOperationContainer(IntPtr javaReference, JniHandleOwnership transfer) : base(
javaReference, transfer)
{
}
public BackgroundOperationContainer(Context context) : base(context)
{
}
public BackgroundOperationContainer(Context context, IAttributeSet attrs) : base(context, attrs)
{
Initialize(attrs);
}
public BackgroundOperationContainer(Context context, IAttributeSet attrs, int defStyle) : base(context,
attrs, defStyle)
{
Initialize(attrs);
}
private void Initialize(IAttributeSet attrs)
{
LayoutInflater inflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
inflater.Inflate(Resource.Layout.background_operation_container, this);
FindViewById(Resource.Id.cancel_background).Click += (obj,args) =>
{
App.Kp2a.CancelBackgroundOperations();
};
}
public void Show()
{
App.Kp2a.UiThreadHandler.Post(() =>
{
Visibility = ViewStates.Visible;
FindViewById<TextView>(Resource.Id.background_ops_message)!.Visibility = ViewStates.Gone;
FindViewById<TextView>(Resource.Id.background_ops_submessage)!.Visibility = ViewStates.Gone;
});
}
public void Hide()
{
App.Kp2a.UiThreadHandler.Post(() =>
{
String activityType = Context.GetType().FullName;
Visibility = ViewStates.Gone;
});
}
public void UpdateMessage(string message)
{
App.Kp2a.UiThreadHandler.Post(() =>
{
TextView messageTextView = FindViewById<TextView>(Resource.Id.background_ops_message)!;
if (string.IsNullOrEmpty(message))
{
messageTextView.Visibility = ViewStates.Gone;
}
else
{
messageTextView.Visibility = ViewStates.Visible;
messageTextView.Text = message;
}
});
}
public void UpdateSubMessage(string submessage)
{
App.Kp2a.UiThreadHandler.Post(() =>
{
TextView subMessageTextView = FindViewById<TextView>(Resource.Id.background_ops_submessage)!;
if (string.IsNullOrEmpty(submessage))
{
subMessageTextView.Visibility = ViewStates.Gone;
}
else
{
subMessageTextView.Visibility = ViewStates.Visible;
subMessageTextView.Text = submessage;
}
});
}
}

View File

@@ -7,7 +7,6 @@ using Android.App;
using Android.Content; using Android.Content;
using Android.Content.Res; using Android.Content.Res;
using Android.Graphics; using Android.Graphics;
using Android.OS;
using Android.Runtime; using Android.Runtime;
using Android.Text; using Android.Text;
using Android.Text.Method; using Android.Text.Method;
@@ -18,6 +17,7 @@ using Android.Views;
using Android.Widget; using Android.Widget;
using Google.Android.Material.Dialog; using Google.Android.Material.Dialog;
using keepass2android; using keepass2android;
using KeePassLib.Interfaces;
namespace keepass2android.views namespace keepass2android.views
{ {

View File

@@ -125,9 +125,12 @@ namespace keepass2android.view
if (_groupBaseActivity.IsFinishing) if (_groupBaseActivity.IsFinishing)
return; return;
_entry = pw; _entry = pw;
_pos = pos; _pos = pos;
try
{
ev.FindViewById(Resource.Id.icon).Visibility = ViewStates.Visible; ev.FindViewById(Resource.Id.icon).Visibility = ViewStates.Visible;
ev.FindViewById(Resource.Id.check_mark).Visibility = ViewStates.Invisible; ev.FindViewById(Resource.Id.check_mark).Visibility = ViewStates.Invisible;
@@ -138,7 +141,8 @@ namespace keepass2android.view
if (isExpired) if (isExpired)
{ {
_db.DrawableFactory.AssignDrawableTo(iv, Context, _db.KpDatabase, PwIcon.Expired, PwUuid.Zero, false); _db.DrawableFactory.AssignDrawableTo(iv, Context, _db.KpDatabase, PwIcon.Expired, PwUuid.Zero, false);
} else }
else
{ {
_db.DrawableFactory.AssignDrawableTo(iv, Context, _db.KpDatabase, pw.IconId, pw.CustomIconUuid, false); _db.DrawableFactory.AssignDrawableTo(iv, Context, _db.KpDatabase, pw.IconId, pw.CustomIconUuid, false);
} }
@@ -184,21 +188,24 @@ namespace keepass2android.view
_textviewDetails.Visibility = ViewStates.Visible; _textviewDetails.Visibility = ViewStates.Visible;
} }
if ( (!_showGroupFullPath) || (!_isSearchResult) ) { if ((!_showGroupFullPath) || (!_isSearchResult))
{
_textgroupFullPath.Visibility = ViewStates.Gone; _textgroupFullPath.Visibility = ViewStates.Gone;
} }
else { else
{
String groupDetail = pw.ParentGroup.GetFullPath(); String groupDetail = pw.ParentGroup.GetFullPath();
if (App.Kp2a.OpenDatabases.Count() > 1) if (App.Kp2a.OpenDatabases.Count() > 1)
{ {
groupDetail += "(" + App.Kp2a.GetFileStorage(_db.Ioc).GetDisplayName(_db.Ioc) + ")"; groupDetail += "(" + App.Kp2a.GetFileStorage(_db.Ioc).GetDisplayName(_db.Ioc) + ")";
} }
var strGroupDetail = new SpannableString (groupDetail); var strGroupDetail = new SpannableString(groupDetail);
if (isExpired) { if (isExpired)
strGroupDetail.SetSpan (new StrikethroughSpan (), 0, groupDetail.Length, SpanTypes.ExclusiveExclusive); {
strGroupDetail.SetSpan(new StrikethroughSpan(), 0, groupDetail.Length, SpanTypes.ExclusiveExclusive);
} }
_textgroupFullPath.TextFormatted = strGroupDetail; _textgroupFullPath.TextFormatted = strGroupDetail;
@@ -208,10 +215,12 @@ namespace keepass2android.view
//try to get totp data //try to get totp data
UpdateTotp(); UpdateTotp();
}
catch (Exception e)
{
Kp2aLog.LogUnexpectedError(e);
}
} }