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

View File

@@ -21,6 +21,7 @@ using System.IO;
using Android;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Preferences;
using KeePassLib.Serialization;
@@ -34,9 +35,17 @@ namespace keepass2android
public static void Log(string message)
{
if (message != null)
Android.Util.Log.Debug("KP2A", message);
if (LogToFile)
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);
}
if (LogToFile)
{
lock (_fileLocker)
{

View File

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

View File

@@ -488,7 +488,7 @@ namespace KeePassLib.Serialization
ProtectedBinary pb = new ProtectedBinary(bProt, pbData,
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);
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
/// </summary>
/// This also contains methods which are UI specific and should be replacable for testing.
public interface IKp2aApp : ICertificateValidationHandler
public interface IKp2aApp : ICertificateValidationHandler, IActiveContextProvider
{
/// <summary>
/// Locks all currently open databases, quicklocking if available (unless false is passed for allowQuickUnlock)
@@ -52,7 +52,9 @@ namespace keepass2android
/// <summary>
/// Loads the specified data as the currently open database, as unlocked.
/// </summary>
Database LoadDatabase(IOConnectionInfo ioConnectionInfo, MemoryStream memoryStream, CompositeKey compKey, ProgressDialogStatusLogger statusLogger, IDatabaseFormat databaseFormat, bool makeCurrent);
Database LoadDatabase(IOConnectionInfo ioConnectionInfo, MemoryStream memoryStream, CompositeKey compKey,
IKp2aStatusLogger statusLogger, IDatabaseFormat databaseFormat, bool makeCurrent,
IDatabaseModificationWatcher modificationWatcher);
HashSet<PwGroup> DirtyGroups { get; }
@@ -96,7 +98,6 @@ namespace keepass2android
EventHandler<DialogClickEventArgs> yesHandler,
EventHandler<DialogClickEventArgs> noHandler,
EventHandler<DialogClickEventArgs> cancelHandler,
Context ctx,
string messageSuffix = "");
/// <summary>
@@ -107,7 +108,6 @@ namespace keepass2android
EventHandler<DialogClickEventArgs> yesHandler,
EventHandler<DialogClickEventArgs> noHandler,
EventHandler<DialogClickEventArgs> cancelHandler,
Context ctx,
string messageSuffix = "");
void ShowMessage(Context ctx, int resourceId, MessageSeverity severity);
@@ -136,10 +136,17 @@ namespace keepass2android
bool CheckForDuplicateUuids { get; }
#if !NoNet && !EXCLUDE_JAVAFILESTORAGE
ICertificateErrorHandler CertificateErrorHandler { get; }
#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);
#endif
Kp2aLog.Log(ex.ToString());
_cacheSupervisor.CouldntOpenFromRemote(ioc, ex);
if (TriggerWarningWhenFallingBackToCache)
{
_cacheSupervisor.CouldntOpenFromRemote(ioc, ex);
}
return File.OpenRead(cachedFilePath);
}
}
@@ -327,7 +330,10 @@ namespace keepass2android.Io
Kp2aLog.Log("couldn't save to remote " + ioc.Path);
Kp2aLog.Log(e.ToString());
//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;
}
}
@@ -632,7 +638,9 @@ namespace keepass2android.Io
set { _cachedStorage.IsOffline = value; }
}
public void OnRequestPermissionsResult(IFileStorageSetupActivity fileStorageSetupActivity, int requestCode,
public bool TriggerWarningWhenFallingBackToCache { get; set; }
public void OnRequestPermissionsResult(IFileStorageSetupActivity fileStorageSetupActivity, int requestCode,
string[] permissions, Permission[] grantResults)
{
_cachedStorage.OnRequestPermissionsResult(fileStorageSetupActivity, requestCode, permissions, grantResults);

View File

@@ -111,6 +111,11 @@ namespace keepass2android.Io
}
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)
{
var ex = new Exception(exception.LocalizedMessage ??

View File

@@ -11,7 +11,8 @@ namespace keepass2android.Io
public interface IOfflineSwitchable
{
bool IsOffline { get; set; }
}
bool TriggerWarningWhenFallingBackToCache { get; set; }
}
/// <summary>
/// Encapsulates another IFileStorage. Allows to switch to offline mode by throwing
@@ -21,8 +22,9 @@ namespace keepass2android.Io
{
private readonly IFileStorage _baseStorage;
public bool IsOffline { get; set; }
public bool TriggerWarningWhenFallingBackToCache { get; set; }
public OfflineSwitchableFileStorage(IFileStorage baseStorage)
public OfflineSwitchableFileStorage(IFileStorage baseStorage)
{
_baseStorage = 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,116 +22,152 @@ using KeePassLib.Interfaces;
namespace keepass2android
{
/// <summary>
/// StatusLogger implementation which shows the progress in a progress dialog
/// </summary>
public class ProgressDialogStatusLogger: IStatusLogger {
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>
/// StatusLogger implementation which shows the progress in a progress dialog
/// </summary>
public class ProgressDialogUi: IProgressUi
{
private readonly IProgressDialog _progressDialog;
readonly IKp2aApp _app;
private readonly Handler _handler;
private string _message = "";
private string _submessage;
private readonly IKp2aApp _app;
public String SubMessage => _submessage;
public String Message => _message;
public String LastSubMessage => _submessage;
public String LastMessage => _message;
public ProgressDialogStatusLogger() {
}
public ProgressDialogStatusLogger(IKp2aApp app, Handler handler, IProgressDialog pd) {
_app = app;
public ProgressDialogUi(IKp2aApp app, Handler handler, IProgressDialog pd)
{
_app = app;
_progressDialog = pd;
_handler = handler;
}
public void UpdateMessage(UiStringKey stringKey) {
if (_app != null)
UpdateMessage(_app.GetResourceString(stringKey));
}
public void UpdateMessage (String message)
{
Kp2aLog.Log("status message: " + message);
public void UpdateSubMessage(String submessage)
{
Kp2aLog.Log("status submessage: " + submessage);
_submessage = submessage;
if (_app != null && _progressDialog != null && _handler != null)
{
_handler.Post(() =>
{
if (!String.IsNullOrEmpty(submessage))
{
_progressDialog.SetMessage(_message + " (" + submessage + ")");
}
else
{
_progressDialog.SetMessage(_message);
}
}
);
}
}
public void Show()
{
_handler.Post(() =>
{
_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); } );
}
}
if (_app != null && _progressDialog != null && _handler != null)
{
_handler.Post(() =>
{
_progressDialog.SetMessage(message);
});
}
}
public void UpdateSubMessage(String submessage)
{
Kp2aLog.Log("status submessage: " + submessage);
_submessage = submessage;
if (_app != null && _progressDialog != null && _handler != null)
{
_handler.Post(() =>
{
if (!String.IsNullOrEmpty(submessage))
{
_progressDialog.SetMessage(_message + " (" + submessage + ")");
}
else
{
_progressDialog.SetMessage(_message);
}
}
);
}
}
}
#region IStatusLogger implementation
public void StartLogging (string strOperation, bool bWriteOperationToLog)
{
}
public void EndLogging ()
{
}
public bool SetProgress (uint uPercent)
{
return true;
}
public bool SetText (string strNewText, LogStatusType lsType)
{
if (strNewText.StartsWith("KP2AKEY_"))
{
UiStringKey key;
if (Enum.TryParse(strNewText.Substring("KP2AKEY_".Length), true, out key))
{
UpdateMessage(_app.GetResourceString(key), lsType);
return true;
}
}
UpdateMessage(strNewText, lsType);
return true;
}
private void UpdateMessage(string message, LogStatusType lsType)
{
if (lsType == LogStatusType.AdditionalInfo)
{
UpdateSubMessage(message);
}
else
{
UpdateMessage(message);
}
}
public bool ContinueWork ()
{
return true;
}
#endregion
}
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -4,109 +4,152 @@ using System.IO;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using KeePassLib.Serialization;
using keepass2android.Io;
using KeePass.Util;
using Group.Pals.Android.Lib.UI.Filechooser.Utils;
using KeePassLib;
namespace keepass2android
{
public class SynchronizeCachedDatabase: RunnableOnFinish
public class SynchronizeCachedDatabase: OperationWithFinishHandler
{
private readonly Activity _context;
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)
{
_context = context;
_app = app;
}
public SynchronizeCachedDatabase(IKp2aApp app, Database database, OnOperationFinishedHandler operationFinishedHandler, IDatabaseModificationWatcher modificationWatcher)
: base(app, operationFinishedHandler)
{
_app = app;
_database = database;
_modificationWatcher = modificationWatcher;
}
public override void Run()
{
try
{
IOConnectionInfo ioc = _app.CurrentDb.Ioc;
IFileStorage fileStorage = _app.GetFileStorage(ioc);
if (!(fileStorage is CachingFileStorage))
{
throw new Exception("Cannot sync a non-cached database!");
}
StatusLogger.UpdateMessage(UiStringKey.SynchronizingCachedDatabase);
CachingFileStorage cachingFileStorage = (CachingFileStorage) fileStorage;
try
{
IOConnectionInfo ioc = _database.Ioc;
IFileStorage fileStorage = _app.GetFileStorage(ioc);
if (!(fileStorage is CachingFileStorage))
{
throw new Exception("Cannot sync a non-cached database!");
}
//download file from remote location and calculate hash:
StatusLogger.UpdateSubMessage(_app.GetResourceString(UiStringKey.DownloadingRemoteFile));
string hash;
MemoryStream remoteData;
try
{
remoteData = cachingFileStorage.GetRemoteDataAndHash(ioc, out hash);
Kp2aLog.Log("Checking for file change. Current hash = " + hash);
}
catch (FileNotFoundException)
{
StatusLogger.UpdateSubMessage(_app.GetResourceString(UiStringKey.RestoringRemoteFile));
cachingFileStorage.UpdateRemoteFile(ioc, _app.GetBooleanPreference(PreferenceKey.UseFileTransactions));
Finish(true, _app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully));
StatusLogger.UpdateMessage(UiStringKey.SynchronizingCachedDatabase);
CachingFileStorage cachingFileStorage = (CachingFileStorage)fileStorage;
//download file from remote location and calculate hash:
StatusLogger.UpdateSubMessage(_app.GetResourceString(UiStringKey.DownloadingRemoteFile));
string hash;
MemoryStream remoteData;
try
{
remoteData = cachingFileStorage.GetRemoteDataAndHash(ioc, out hash);
Kp2aLog.Log("Checking for file change. Current hash = " + hash);
}
catch (FileNotFoundException)
{
StatusLogger.UpdateSubMessage(_app.GetResourceString(UiStringKey.RestoringRemoteFile));
cachingFileStorage.UpdateRemoteFile(ioc,
_app.GetBooleanPreference(PreferenceKey.UseFileTransactions));
Finish(true, _app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully));
Kp2aLog.Log("Checking for file change: file not found");
return;
}
return;
}
//check if remote file was modified:
//check if remote file was modified:
var baseVersionHash = cachingFileStorage.GetBaseVersionHash(ioc);
Kp2aLog.Log("Checking for file change. baseVersionHash = " + baseVersionHash);
if (baseVersionHash != hash)
{
//remote file is modified
if (cachingFileStorage.HasLocalChanges(ioc))
{
//conflict! need to merge
_saveDb = new SaveDb(_context, _app, new ActionOnFinish(ActiveActivity, (success, result, activity) =>
{
if (!success)
{
Finish(false, result);
}
else
{
Finish(true, _app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully));
}
_saveDb = null;
}), _app.CurrentDb, false, remoteData);
_saveDb.Run();
if (baseVersionHash != hash)
{
//remote file is modified
if (cachingFileStorage.HasLocalChanges(ioc))
{
//conflict! need to merge
var _saveDb = new SaveDb(_app, new ActionOnOperationFinished(_app,
(success, result, activity) =>
{
if (!success)
{
Finish(false, result);
}
else
{
Finish(true, _app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully));
}
}), _database, false, remoteData, _modificationWatcher);
_saveDb.SetStatusLogger(StatusLogger);
_saveDb.DoNotSetStatusLoggerMessage = true; //Keep "sync db" as main message
_saveDb.SyncInBackground = false;
_saveDb.Run();
_app.CurrentDb.UpdateGlobals();
_database.UpdateGlobals();
_app.MarkAllGroupsAsDirty();
}
else
{
//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.)
_app.TriggerReload(_context, (bool result) => Finish(result));
}
}
else
{
//remote file is unmodified
if (cachingFileStorage.HasLocalChanges(ioc))
{
//but we have local changes -> upload:
StatusLogger.UpdateSubMessage(_app.GetResourceString(UiStringKey.UploadingFile));
cachingFileStorage.UpdateRemoteFile(ioc, _app.GetBooleanPreference(PreferenceKey.UseFileTransactions));
StatusLogger.UpdateSubMessage("");
Finish(true, _app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully));
}
else
{
//files are in sync: just set the result
Finish(true, _app.GetResourceString(UiStringKey.FilesInSync));
}
}
}
_app.MarkAllGroupsAsDirty();
}
else
{
//only the remote file was modified -> reload database.
var onFinished = new ActionOnOperationFinished(_app, (success, result, activity) =>
{
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
{
//remote file is unmodified
if (cachingFileStorage.HasLocalChanges(ioc))
{
//but we have local changes -> upload:
StatusLogger.UpdateSubMessage(_app.GetResourceString(UiStringKey.UploadingFile));
cachingFileStorage.UpdateRemoteFile(ioc,
_app.GetBooleanPreference(PreferenceKey.UseFileTransactions));
StatusLogger.UpdateSubMessage("");
Finish(true, _app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully));
}
else
{
//files are in sync: just set the result
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)
{
Kp2aLog.LogUnexpectedError(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
{
public class AddEntry : RunnableOnFinish {
public class AddEntry : OperationWithFinishHandler {
protected Database Db
{
get { return _app.CurrentDb; }
@@ -30,22 +30,20 @@ namespace keepass2android
private readonly IKp2aApp _app;
private readonly PwEntry _entry;
private readonly PwGroup _parentGroup;
private readonly Activity _ctx;
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) {
_ctx = ctx;
public AddEntry(Database db, IKp2aApp app, PwEntry entry, PwGroup parentGroup, OnOperationFinishedHandler operationFinishedHandler):base(app, operationFinishedHandler) {
_db = db;
_parentGroup = parentGroup;
_app = app;
_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);
// Commit to disk
SaveDb save = new SaveDb(_ctx, _app, _app.CurrentDb, OnFinishToRun);
SaveDb save = new SaveDb(_app, _app.CurrentDb, operationFinishedHandler);
save.SetStatusLogger(StatusLogger);
save.Run();
}
private class AfterAdd : OnFinish {
private class AfterAdd : OnOperationFinishedHandler {
private readonly Database _db;
private readonly PwEntry _entry;
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;
_entry = entry;
_app = app;

View File

@@ -23,7 +23,7 @@ using KeePassLib;
namespace keepass2android
{
public class AddGroup : RunnableOnFinish {
public class AddGroup : OperationWithFinishHandler {
internal Database Db
{
get { return _app.CurrentDb; }
@@ -38,18 +38,16 @@ namespace keepass2android
public PwGroup Group;
internal PwGroup Parent;
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) {
return new AddGroup(ctx, app, name, iconid, groupCustomIconId, parent, finish, dontSave);
public static AddGroup GetInstance(IKp2aApp app, string name, int iconid, PwUuid groupCustomIconId, PwGroup parent, OnOperationFinishedHandler operationFinishedHandler, bool 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)
: base(ctx, finish)
private AddGroup(IKp2aApp app, String name, int iconid, PwUuid groupCustomIconId, PwGroup parent, OnOperationFinishedHandler operationFinishedHandler, bool dontSave)
: base(app, operationFinishedHandler)
{
_ctx = ctx;
_name = name;
_iconId = iconid;
_groupCustomIconId = groupCustomIconId;
@@ -57,7 +55,7 @@ namespace keepass2android
DontSave = dontSave;
_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);
// 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.Run();
}
private class AfterAdd : OnFinish {
private class AfterAdd : OnOperationFinishedHandler {
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;
}

View File

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

View File

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

View File

@@ -26,27 +26,24 @@ using KeePassLib.Keys;
namespace keepass2android
{
public class CreateDb : RunnableOnFinish {
public class CreateDb : OperationWithFinishHandler {
private readonly IOConnectionInfo _ioc;
private readonly bool _dontSave;
private readonly Activity _ctx;
private readonly IKp2aApp _app;
private CompositeKey _key;
private readonly bool _makeCurrent;
public CreateDb(IKp2aApp app, Activity ctx, IOConnectionInfo ioc, OnFinish finish, bool dontSave, bool makeCurrent): base(ctx, finish) {
_ctx = ctx;
_ioc = ioc;
public CreateDb(IKp2aApp app, Activity ctx, IOConnectionInfo ioc, OnOperationFinishedHandler operationFinishedHandler, bool dontSave, bool makeCurrent): base(app, operationFinishedHandler) {
_ioc = ioc;
_dontSave = dontSave;
_makeCurrent = makeCurrent;
_app = app;
}
public CreateDb(IKp2aApp app, Activity ctx, IOConnectionInfo ioc, OnFinish finish, bool dontSave, CompositeKey key, bool makeCurrent)
: base(ctx, finish)
public CreateDb(IKp2aApp app, Activity ctx, IOConnectionInfo ioc, OnOperationFinishedHandler operationFinishedHandler, bool dontSave, CompositeKey key, bool makeCurrent)
: base(app, operationFinishedHandler)
{
_ctx = ctx;
_ioc = ioc;
_ioc = ioc;
_dontSave = dontSave;
_app = app;
_key = key;
@@ -77,19 +74,19 @@ namespace keepass2android
db.SearchHelper = new SearchDbHelper(_app);
// 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();
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();
List<PwEntry> addedEntries;
AddTemplateEntries addTemplates = new AddTemplateEntries(_ctx, _app, null);
AddTemplateEntries addTemplates = new AddTemplateEntries(_app, null);
addTemplates.AddTemplates(out addedEntries);
// Commit changes
SaveDb save = new SaveDb(_ctx, _app, db, OnFinishToRun, _dontSave);
SaveDb save = new SaveDb(_app, db, operationFinishedHandler, _dontSave, null);
save.SetStatusLogger(StatusLogger);
_onFinishToRun = null;
_operationFinishedHandler = null;
save.Run();
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 UiStringKey _statusMessage;
public DeleteEntry(Activity activiy, IKp2aApp app, PwEntry entry, OnFinish finish):base(activiy, finish, app) {
Ctx = activiy;
public DeleteEntry(IKp2aApp app, PwEntry entry, OnOperationFinishedHandler operationFinishedHandler):base(operationFinishedHandler, app) {
Db = app.FindDatabaseForElement(entry);
_entry = entry;

View File

@@ -29,25 +29,25 @@ namespace keepass2android
private PwGroup _group;
protected bool DontSave;
public DeleteGroup(Activity activity, IKp2aApp app, PwGroup group, OnFinish finish)
: base(activity, finish, app)
public DeleteGroup(Activity activity, IKp2aApp app, PwGroup group, OnOperationFinishedHandler operationFinishedHandler)
: 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)
: base(finish)
public DeleteGroup(Context ctx, Database db, PwGroup group, Activity act, OnOperationFinishedHandler operationFinishedHandler, bool dontSave)
: base(operationFinishedHandler)
{
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);
}
*/
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;
DontSave = dontSave;

View File

@@ -12,11 +12,11 @@ namespace keepass2android
private readonly List<IStructureItem> _elementsToDelete;
private readonly bool _canRecycle;
public DeleteMultipleItemsFromOneDatabase(Activity activity, Database db, List<IStructureItem> elementsToDelete, OnFinish finish, IKp2aApp app)
: base(activity, finish, app)
public DeleteMultipleItemsFromOneDatabase(Activity activity, Database db, List<IStructureItem> elementsToDelete, OnOperationFinishedHandler operationFinishedHandler, IKp2aApp app)
: base(operationFinishedHandler, app)
{
_elementsToDelete = elementsToDelete;
SetMembers(activity, db);
SetMembers(db);
//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)

View File

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

View File

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

View File

@@ -21,10 +21,10 @@ using Android.App;
namespace keepass2android
{
public abstract class FileOnFinish : OnFinish {
public abstract class FileOnFinish : OnOperationFinishedHandler {
private String _filename = "";
protected FileOnFinish(Activity activity, FileOnFinish finish):base(activity, finish) {
protected FileOnFinish(IKp2aApp app, FileOnFinish operationFinishedHandler):base(app, operationFinishedHandler) {
}
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,60 +21,88 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Android.App;
using Android.OS;
using KeePass.Util;
using keepass2android.database.edit;
using keepass2android.Io;
using KeePassLib;
using KeePassLib.Keys;
using KeePassLib.Serialization;
namespace keepass2android
{
public class LoadDb : RunnableOnFinish {
public class LoadDb : OperationWithFinishHandler {
private readonly IOConnectionInfo _ioc;
private readonly Task<MemoryStream> _databaseData;
private readonly CompositeKey _compositeKey;
private readonly string _keyfileOrProvider;
private readonly string? _keyfileOrProvider;
private readonly IKp2aApp _app;
private readonly bool _rememberKeyfile;
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)
{
_app = app;
_modificationWatcher = modificationWatcher ?? new NullDatabaseModificationWatcher();
_app = app;
_ioc = ioc;
_databaseData = databaseData;
_compositeKey = compositeKey;
_keyfileOrProvider = keyfileOrProvider;
_updateLastUsageTimestamp = updateLastUsageTimestamp;
_makeCurrent = makeCurrent;
_rememberKeyfile = app.GetBooleanPreference(PreferenceKey.remember_keyfile);
}
_rememberKeyfile = app.GetBooleanPreference(PreferenceKey.remember_keyfile);
}
protected bool success = false;
private bool _updateLastUsageTimestamp;
private readonly bool _makeCurrent;
private readonly IDatabaseModificationWatcher _modificationWatcher;
public override void Run()
public override void Run()
{
try
{
try
{
//make sure the file data is stored in the recent files list even if loading fails
SaveFileData(_ioc, _keyfileOrProvider);
SaveFileData(_ioc, _keyfileOrProvider);
var fileStorage = _app.GetFileStorage(_ioc);
RequiresSubsequentSync = false;
StatusLogger.UpdateMessage(UiStringKey.loading_database);
if (!DoNotSetStatusLoggerMessage)
{
StatusLogger.UpdateMessage(UiStringKey.loading_database);
}
//get the stream data into a single stream variable (databaseStream) regardless whether its preloaded or not:
MemoryStream preloadedMemoryStream = _databaseData == null ? null : _databaseData.Result;
MemoryStream databaseStream;
if (preloadedMemoryStream != null)
databaseStream = preloadedMemoryStream;
else
if (preloadedMemoryStream != null)
{
//note: if the stream has been loaded already, we don't need to trigger another sync later on
databaseStream = preloadedMemoryStream;
}
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();
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:
_format = new KdbxDatabaseFormat(KdbxDatabaseFormat.GetFormatToUse(_app.GetFileStorage(_ioc).GetFileExtension(_ioc)));
_format = new KdbxDatabaseFormat(KdbxDatabaseFormat.GetFormatToUse(fileStorage.GetFileExtension(_ioc)));
TryLoad(databaseStream);
@@ -120,7 +153,13 @@ namespace keepass2android
Finish(false, _app.GetResourceString(UiStringKey.DuplicateUuidsError) + " " + ExceptionUtil.GetErrorMessage(e) + _app.GetResourceString(UiStringKey.DuplicateUuidsErrorAdditional), false, Exception);
return;
}
catch (Exception e)
catch (Java.Lang.InterruptedException)
{
Kp2aLog.Log("Load interrupted");
//close without Finish()
return;
}
catch (Exception e)
{
if (!(e is InvalidCompositeKeyException))
Kp2aLog.LogUnexpectedError(e);
@@ -131,7 +170,9 @@ namespace keepass2android
}
/// <summary>
public bool RequiresSubsequentSync { get; set; } = false;
/// <summary>
/// Holds the exception which was thrown during execution (if any)
/// </summary>
public Exception Exception { get; set; }
@@ -146,15 +187,21 @@ namespace keepass2android
workingCopy.Seek(0, SeekOrigin.Begin);
//reset stream if we need to reuse it later:
databaseStream.Seek(0, SeekOrigin.Begin);
if (!StatusLogger.ContinueWork())
{
throw new Java.Lang.InterruptedException();
}
//now let's go:
try
{
Database newDb = _app.LoadDatabase(_ioc, workingCopy, _compositeKey, StatusLogger, _format, _makeCurrent);
Kp2aLog.Log("LoadDB OK");
try
{
Database newDb =
_app.LoadDatabase(_ioc, workingCopy, _compositeKey, StatusLogger, _format, _makeCurrent, _modificationWatcher);
Kp2aLog.Log("LoadDB OK");
Finish(true, _format.SuccessMessage);
return newDb;
}
return newDb;
}
catch (OldFormatException)
{
_format = new KdbDatabaseFormat(_app);

View File

@@ -10,18 +10,16 @@ using KeePassLib.Interfaces;
namespace keepass2android.database.edit
{
public class MoveElements: RunnableOnFinish
public class MoveElements: OperationWithFinishHandler
{
private readonly List<IStructureItem> _elementsToMove;
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;
_targetGroup = targetGroup;
_ctx = ctx;
_app = app;
}
@@ -123,24 +121,24 @@ namespace keepass2android.database.edit
int indexToSave = 0;
bool allSavesSuccess = true;
void ContinueSave(bool success, string message, Activity activeActivity)
void ContinueSave(bool success, string message, Context activeActivity)
{
allSavesSuccess &= success;
indexToSave++;
if (indexToSave == allDatabasesToSave.Count)
{
OnFinishToRun.SetResult(allSavesSuccess);
OnFinishToRun.Run();
operationFinishedHandler.SetResult(allSavesSuccess);
operationFinishedHandler.Run();
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.ShowDatabaseIocInStatus = allDatabasesToSave.Count > 1;
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.ShowDatabaseIocInStatus = allDatabasesToSave.Count > 1;
save.Run();

View File

@@ -22,10 +22,16 @@ using Android.Content;
using Android.OS;
using Android.Widget;
using Google.Android.Material.Dialog;
using KeePassLib.Interfaces;
namespace keepass2android
{
public abstract class OnFinish
public interface IActiveContextProvider
{
Context ActiveContext { get; }
}
public abstract class OnOperationFinishedHandler
{
protected bool Success;
protected String Message;
@@ -37,63 +43,41 @@ namespace keepass2android
set;
}
protected OnFinish BaseOnFinish;
protected Context ActiveContext
{
get
{
return _activeContextProvider?.ActiveContext;
}
}
protected OnOperationFinishedHandler NextOnOperationFinishedHandler;
protected Handler Handler;
private ProgressDialogStatusLogger _statusLogger = new ProgressDialogStatusLogger(); //default: no logging but not null -> can be used whenever desired
private Activity _activeActivity, _previouslyActiveActivity;
private IKp2aStatusLogger _statusLogger = new Kp2aNullStatusLogger(); //default: no logging but not null -> can be used whenever desired
private readonly IActiveContextProvider _activeContextProvider;
public ProgressDialogStatusLogger StatusLogger
public IKp2aStatusLogger StatusLogger
{
get { return _statusLogger; }
set { _statusLogger = value; }
}
public Activity ActiveActivity
{
get { return _activeActivity; }
set
{
if (_activeActivity != null && _activeActivity != _previouslyActiveActivity)
{
_previouslyActiveActivity = _activeActivity;
}
_activeActivity = value;
if (BaseOnFinish != null)
{
BaseOnFinish.ActiveActivity = value;
}
}
}
public Activity PreviouslyActiveActivity
} protected OnOperationFinishedHandler(IActiveContextProvider activeContextProvider, Handler handler)
{
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;
_activeContextProvider = activeContextProvider;
NextOnOperationFinishedHandler = null;
Handler = handler;
}
protected OnFinish(Activity activeActivity, OnFinish finish)
protected OnOperationFinishedHandler(IActiveContextProvider activeContextProvider, OnOperationFinishedHandler operationFinishedHandler, Handler handler)
{
ActiveActivity = activeActivity;
BaseOnFinish = finish;
_activeContextProvider = activeContextProvider;
NextOnOperationFinishedHandler = operationFinishedHandler;
Handler = handler;
}
protected OnOperationFinishedHandler(IActiveContextProvider activeContextProvider, OnOperationFinishedHandler operationFinishedHandler)
{
_activeContextProvider = activeContextProvider;
NextOnOperationFinishedHandler = operationFinishedHandler;
Handler = null;
}
@@ -110,14 +94,19 @@ namespace keepass2android
}
public virtual void Run() {
if (BaseOnFinish == null) return;
if (NextOnOperationFinishedHandler == null) return;
// Pass on result on call finish
BaseOnFinish.SetResult(Success, Message, ImportantMessage, Exception);
if ( Handler != null ) {
Handler.Post(BaseOnFinish.Run);
NextOnOperationFinishedHandler.SetResult(Success, Message, ImportantMessage, Exception);
var handler = Handler ?? NextOnOperationFinishedHandler.Handler ?? null;
if (handler != null ) {
handler.Post(() =>
{
NextOnOperationFinishedHandler.Run();
});
} else {
BaseOnFinish.Run();
NextOnOperationFinishedHandler.Run();
}
}
@@ -128,7 +117,7 @@ namespace keepass2android
public static void DisplayMessage(Context ctx, string message, bool makeDialog)
{
if ( !String.IsNullOrEmpty(message) ) {
Kp2aLog.Log("OnFinish message: " + message);
Kp2aLog.Log("OnOperationFinishedHandler message: " + message);
if (makeDialog && ctx != null)
{
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,57 +30,69 @@ using keepass2android.Io;
using Debug = System.Diagnostics.Debug;
using Exception = System.Exception;
using KeePass.Util;
using Thread = System.Threading.Thread;
namespace keepass2android
{
/// <summary>
/// Save the database. If the file has changed, ask the user if he wants to overwrite or sync.
/// </summary>
public class SaveDb : RunnableOnFinish {
public class SaveDb : OperationWithFinishHandler {
private readonly IKp2aApp _app;
private readonly Database _db;
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>
/// 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>
private readonly Stream _streamForOrigFile;
private readonly Context _ctx;
private Java.Lang.Thread _workerThread;
public SaveDb(Activity ctx, IKp2aApp app, Database db, OnFinish finish, bool dontSave)
: base(ctx, finish)
public SaveDb(IKp2aApp app, Database db, OnOperationFinishedHandler operationFinishedHandler, bool dontSave, IDatabaseModificationWatcher modificationWatcher)
: base(app, operationFinishedHandler)
{
_db = db;
_ctx = ctx;
_app = app;
_dontSave = dontSave;
_modificationWatcher = modificationWatcher ?? new NullDatabaseModificationWatcher();
}
/// <summary>
/// Constructor for sync
/// </summary>
/// <param name="ctx"></param>
/// <param name="app"></param>
/// <param name="finish"></param>
/// <param name="operationFinishedHandler"></param>
/// <param name="dontSave"></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)
: base(ctx, finish)
public SaveDb(IKp2aApp app, OnOperationFinishedHandler operationFinishedHandler, Database db, bool dontSave, Stream streamForOrigFile, IDatabaseModificationWatcher modificationWatcher = null)
: base(app, operationFinishedHandler)
{
_db = db;
_ctx = ctx;
_modificationWatcher = modificationWatcher ?? new NullDatabaseModificationWatcher();
_db = db;
_app = app;
_dontSave = dontSave;
_streamForOrigFile = streamForOrigFile;
}
SyncInBackground = _app.SyncInBackgroundPreference;
public SaveDb(Activity ctx, IKp2aApp app, Database db, OnFinish finish)
: base(ctx, finish)
}
public SaveDb(IKp2aApp app, Database db, OnOperationFinishedHandler operationFinishedHandler, IDatabaseModificationWatcher modificationWatcher = null)
: base(app, operationFinishedHandler)
{
_ctx = ctx;
_app = app;
_modificationWatcher = modificationWatcher ?? new NullDatabaseModificationWatcher();
_app = app;
_db = db;
_dontSave = false;
}
SyncInBackground = _app.SyncInBackgroundPreference;
}
public bool ShowDatabaseIocInStatus { get; set; }
@@ -103,29 +115,42 @@ namespace keepass2android
if (ShowDatabaseIocInStatus)
message += " (" + _app.GetFileStorage(_db.Ioc).GetDisplayName(_db.Ioc) + ")";
StatusLogger.UpdateMessage(message);
IOConnectionInfo ioc = _db.Ioc;
if (!DoNotSetStatusLoggerMessage)
{
StatusLogger.UpdateMessage(message);
}
IOConnectionInfo ioc = _db.Ioc;
IFileStorage fileStorage = _app.GetFileStorage(ioc);
if (_streamForOrigFile == null)
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 ((!_app.GetBooleanPreference(PreferenceKey.CheckForFileChangesOnSave))
|| (_db.KpDatabase.HashOfFileOnDisk == null)) //first time saving
{
PerformSaveWithoutCheck(fileStorage, ioc);
Finish(true);
FinishWithSuccess();
return;
}
}
bool hasStreamForOrigFile = (_streamForOrigFile != null);
bool hasChangeFast = hasStreamForOrigFile ||
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) ==
FileHashChange.Changed); //if that fails, hash the file and compare:
FileHashChange.Changed)); //if that fails, hash the file and compare:
if (hasHashChanged)
{
@@ -158,15 +183,14 @@ namespace keepass2android
RunInWorkerThread(() =>
{
PerformSaveWithoutCheck(fileStorage, ioc);
Finish(true);
FinishWithSuccess();
});
},
//cancel
(sender, args) =>
{
RunInWorkerThread(() => Finish(false));
},
_ctx
}
);
}
@@ -174,7 +198,7 @@ namespace keepass2android
else
{
PerformSaveWithoutCheck(fileStorage, ioc);
Finish(true);
FinishWithSuccess();
}
}
@@ -194,22 +218,67 @@ namespace keepass2android
}
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)
{
//note: when synced, the file might be downloaded once again from the server. Caching the data
//in the hashing function would solve this but increases complexity. I currently assume the files are
//small.
MergeIn(fileStorage, ioc);
try
{
_modificationWatcher.BeforeModifyDatabases();
}
catch (Java.Lang.InterruptedException)
{
// leave without Finish()
return;
}
try
{
MergeIn(fileStorage, ioc);
}
finally
{
_modificationWatcher.AfterModifyDatabases();
}
PerformSaveWithoutCheck(fileStorage, ioc);
_db.UpdateGlobals();
Finish(true);
new Handler(Looper.MainLooper).Post(() =>
{
_db.UpdateGlobals();
});
FinishWithSuccess();
}
private void RunInWorkerThread(Action runHandler)
{
try
@@ -282,7 +351,7 @@ namespace keepass2android
private void PerformSaveWithoutCheck(IFileStorage fileStorage, IOConnectionInfo ioc)
{
StatusLogger.UpdateSubMessage("");
_db.SaveData();
_db.SaveData(fileStorage);
_db.LastFileVersion = fileStorage.GetCurrentFileVersionFast(ioc);
}

View File

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

View File

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

View File

@@ -228,9 +228,9 @@ namespace keepass2android
newEntry.SetUuid(new PwUuid(true), true); // Create new UUID
string strTitle = newEntry.Strings.ReadSafe(PwDefs.TitleField);
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();
}
@@ -260,9 +260,9 @@ namespace keepass2android
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();
}
@@ -343,7 +343,7 @@ namespace keepass2android
}
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();
autoOpenGroup = addGroupTask.Group;
}
@@ -367,9 +367,9 @@ namespace keepass2android
{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();
}

View File

@@ -213,10 +213,9 @@ namespace keepass2android
}
// Create the new database
CreateDb create = new CreateDb(App.Kp2a, this, _ioc, new LaunchGroupActivity(_ioc, this), false, newKey, makeCurrent);
ProgressTask createTask = new ProgressTask(
App.Kp2a,
this, create);
CreateDb create = new CreateDb(App.Kp2a, this, _ioc, new LaunchGroupActivity(_ioc, App.Kp2a, this), false, newKey, makeCurrent);
BlockingOperationStarter createTask = new BlockingOperationStarter(
App.Kp2a, create);
createTask.Run();
}
@@ -317,7 +316,7 @@ namespace keepass2android
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_confpassword).Text = generatedPassword;
}
@@ -403,14 +402,14 @@ namespace keepass2android
private class LaunchGroupActivity : FileOnFinish
{
readonly CreateDatabaseActivity _activity;
private readonly IOConnectionInfo _ioc;
private readonly CreateDatabaseActivity _activity;
public LaunchGroupActivity(IOConnectionInfo ioc, CreateDatabaseActivity activity)
: base(activity, null)
public LaunchGroupActivity(IOConnectionInfo ioc, IKp2aApp app, CreateDatabaseActivity activity)
: base(app, null)
{
_activity = activity;
_ioc = ioc;
_activity = activity;
_ioc = ioc;
}
public override void Run()
@@ -420,7 +419,7 @@ namespace keepass2android
// Update the ongoing notification
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
FileDbHelper dbHelper = App.Kp2a.FileDbHelper;

View File

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

View File

@@ -56,6 +56,7 @@ using Android.Util;
using AndroidX.Core.Content;
using Google.Android.Material.Dialog;
using keepass2android;
using keepass2android.views;
namespace keepass2android
{
@@ -63,7 +64,7 @@ namespace keepass2android
{
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;
}
@@ -75,13 +76,13 @@ namespace keepass2android
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)
App.Kp2a.ShowMessage(activity, message, MessageSeverity.Error);
App.Kp2a.ShowMessage(context, message, MessageSeverity.Error);
}
), ((EntryActivity)_activity).Entry.Binaries.Get(_binaryToSave), ioc);
ProgressTask pt = new ProgressTask(App.Kp2a, _activity, task);
BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, task);
pt.Run();
}
@@ -89,6 +90,7 @@ namespace keepass2android
public override void OnSaveInstanceState(Bundle outState)
{
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,
Theme = "@style/Kp2aTheme_ActionBar")]
public class EntryActivity : LockCloseActivity
public class EntryActivity : LockCloseActivity, IProgressUiProvider
{
public const String KeyEntry = "entry";
public const String KeyRefreshPos = "refresh_pos";
@@ -110,6 +112,45 @@ namespace keepass2android
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)
{
Intent i = new Intent(act, typeof(EntryActivity));
@@ -481,8 +522,8 @@ namespace keepass2android
Entry.Expires = true;
Entry.Touch(true);
RequiresRefresh();
UpdateEntry update = new UpdateEntry(this, App.Kp2a, backupEntry, Entry, null);
ProgressTask pt = new ProgressTask(App.Kp2a, this, update);
UpdateEntry update = new UpdateEntry(App.Kp2a, backupEntry, Entry, null);
BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, update);
pt.Run();
}
FillData();
@@ -501,7 +542,13 @@ namespace keepass2android
//the rest of the things to do depends on the current app task:
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()
{
@@ -525,13 +572,17 @@ namespace keepass2android
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) =>
{
activity.SetResult(KeePass.ExitRefresh);
activity.Finish();
if (context is Activity activity)
{
activity.SetResult(KeePass.ExitRefresh);
activity.Finish();
}
}));
ProgressTask pt = new ProgressTask(App.Kp2a, this, saveTask);
BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, saveTask);
pt.Run();
}
@@ -1078,7 +1129,9 @@ namespace keepass2android
UnregisterReceiver(_pluginActionReceiver);
if (_pluginFieldReceiver != null)
UnregisterReceiver(_pluginFieldReceiver);
base.OnDestroy();
if (_dataUpdatedIntentReceiver != null)
UnregisterReceiver(_dataUpdatedIntentReceiver);
base.OnDestroy();
}
private void NotifyPluginsOnClose()
@@ -1260,13 +1313,13 @@ namespace keepass2android
}
public class WriteBinaryTask : RunnableOnFinish
public class WriteBinaryTask : OperationWithFinishHandler
{
private readonly IKp2aApp _app;
private readonly ProtectedBinary _data;
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;
_data = data;
@@ -1354,6 +1407,7 @@ namespace keepass2android
}
bool isPaused = false;
private UpdateEntryActivityBroadcastReceiver _dataUpdatedIntentReceiver;
protected override void OnPause()
{
@@ -1440,8 +1494,8 @@ namespace keepass2android
Finish();
return true;
case Resource.Id.menu_delete:
DeleteEntry task = new DeleteEntry(this, App.Kp2a, Entry,
new ActionOnFinish(this, (success, message, activity) => { if (success) { RequiresRefresh(); Finish();}}));
DeleteEntry task = new DeleteEntry(App.Kp2a, Entry,
new ActionOnOperationFinished(App.Kp2a, (success, message, context) => { if (success) { RequiresRefresh(); Finish();}}));
task.Start();
break;
case Resource.Id.menu_toggle_pass:
@@ -1504,16 +1558,16 @@ namespace keepass2android
//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);
finishAction((EntryActivity)activity);
OnOperationFinishedHandler.DisplayMessage(this, message, true);
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();
}
@@ -1603,5 +1657,7 @@ namespace keepass2android
imageViewerIntent.PutExtra("EntryKey", key);
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>
/// 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
/// when there are big attachements.
/// </summary>

View File

@@ -19,23 +19,23 @@ namespace keepass2android
{
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;
}
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)
App.Kp2a.ShowMessage(activity, message, MessageSeverity.Error);
App.Kp2a.ShowMessage(context, message, MessageSeverity.Error);
else
App.Kp2a.ShowMessage(activity, _activity.GetString(Resource.String.export_database_successful), MessageSeverity.Info);
activity.Finish();
App.Kp2a.ShowMessage(context, _activity.GetString(Resource.String.export_database_successful), MessageSeverity.Info);
(context as Activity)?.Finish();
}
), _ffp, ioc);
ProgressTask pt = new ProgressTask(App.Kp2a, _activity, exportDb);
BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, exportDb);
pt.Run();
}
@@ -93,13 +93,13 @@ namespace keepass2android
get { return 0; }
}
public class ExportDb : RunnableOnFinish
public class ExportDb : OperationWithFinishHandler
{
private readonly IKp2aApp _app;
private readonly FileFormatProvider _fileFormat;
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;
this._fileFormat = fileFormat;

View File

@@ -12,9 +12,9 @@ namespace keepass2android
{
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;
_activity = activity;
@@ -103,7 +103,7 @@ namespace keepass2android
}
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)
{
@@ -115,7 +115,7 @@ namespace keepass2android
}), filename);
new ProgressTask(App.Kp2a, _activity, task).Run();
new BlockingOperationStarter(App.Kp2a, task).Run();
}
return true;

View File

@@ -667,10 +667,10 @@ namespace keepass2android
return true;
}
private void IocSelected(Activity activity, IOConnectionInfo ioc)
private void IocSelected(Context context, IOConnectionInfo ioc)
{
if (OnOpen != null)
OnOpen(activity, ioc);
OnOpen(context, ioc);
}
public bool StartFileChooser(string defaultPath)
@@ -781,7 +781,7 @@ namespace keepass2android
}
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)
{
@@ -793,7 +793,7 @@ namespace keepass2android
}), filename);
new ProgressTask(App.Kp2a, activity, task).Run();
new BlockingOperationStarter(App.Kp2a, task).Run();
}
}

View File

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

View File

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

View File

@@ -43,11 +43,13 @@ using keepass2android;
using KeeTrayTOTP.Libraries;
using AndroidX.AppCompat.Widget;
using Google.Android.Material.Dialog;
using keepass2android.views;
using SearchView = AndroidX.AppCompat.Widget.SearchView;
using AndroidX.Core.Content;
namespace keepass2android
{
public abstract class GroupBaseActivity : LockCloseActivity
public abstract class GroupBaseActivity : LockCloseActivity, IProgressUiProvider
{
public const String KeyEntry = "entry";
public const String KeyMode = "mode";
@@ -201,19 +203,18 @@ namespace keepass2android
new PwUuid(MemUtil.HexStringToByteArray(data.Extras.GetString(GroupEditActivity.KeyCustomIconId)));
String strGroupUuid = data.Extras.GetString(GroupEditActivity.KeyGroupUuid);
GroupBaseActivity act = this;
Handler handler = new Handler();
RunnableOnFinish task;
OperationWithFinishHandler task;
if (strGroupUuid == null)
{
task = AddGroup.GetInstance(this, App.Kp2a, groupName, groupIconId, groupCustomIconId, Group, new RefreshTask(handler, this), false);
task = AddGroup.GetInstance(App.Kp2a, groupName, groupIconId, groupCustomIconId, Group, CreateRefreshAction(), false);
}
else
{
PwUuid groupUuid = new PwUuid(MemUtil.HexStringToByteArray(strGroupUuid));
task = new EditGroup(this, App.Kp2a, groupName, (PwIcon)groupIconId, groupCustomIconId, App.Kp2a.FindGroup(groupUuid),
new RefreshTask(handler, this));
task = new EditGroup(App.Kp2a, groupName, (PwIcon)groupIconId, groupCustomIconId, App.Kp2a.FindGroup(groupUuid),
CreateRefreshAction());
}
ProgressTask pt = new ProgressTask(App.Kp2a, act, task);
BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, task);
pt.Run();
}
@@ -274,6 +275,7 @@ namespace keepass2android
private IMenuItem searchItem;
private IMenuItem searchItemDummy;
private bool isPaused;
private UpdateGroupBaseActivityBroadcastReceiver _dataUpdatedIntentReceiver;
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);
@@ -924,14 +927,14 @@ namespace keepass2android
var moveElement = new MoveElements(elementsToMove.ToList(), Group, this, App.Kp2a, new ActionOnFinish(this,
(success, message, activity) =>
var moveElement = new MoveElements(elementsToMove.ToList(), Group, App.Kp2a, new ActionInContextInstanceOnOperationFinished(ContextInstanceId, App.Kp2a,
(success, message, context) =>
{
((GroupBaseActivity)activity)?.StopMovingElements();
(context as GroupBaseActivity)?.StopMovingElements();
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();
}
@@ -1033,6 +1036,13 @@ namespace keepass2android
}
}
protected override void OnDestroy()
{
UnregisterReceiver(_dataUpdatedIntentReceiver);
base.OnDestroy();
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
@@ -1212,7 +1222,7 @@ namespace keepass2android
return true;
case Resource.Id.menu_sync:
new SyncUtil(this).SynchronizeDatabase(() => { });
new SyncUtil(this).StartSynchronizeDatabase(App.Kp2a.CurrentDb.Ioc);
return true;
case Resource.Id.menu_work_offline:
@@ -1223,7 +1233,7 @@ namespace keepass2android
case Resource.Id.menu_work_online:
App.Kp2a.OfflineMode = App.Kp2a.OfflineModePreference = false;
UpdateOfflineModeMenu();
new SyncUtil(this).SynchronizeDatabase(() => { });
new SyncUtil(this).StartSynchronizeDatabase(App.Kp2a.CurrentDb.Ioc);
return true;
case Resource.Id.menu_open_other_db:
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)
{
@@ -1408,6 +1374,79 @@ namespace keepass2android
{
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
@@ -1470,12 +1509,12 @@ namespace keepass2android
{
return false;
}
Handler handler = new Handler();
switch (item.ItemId)
{
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;
case Resource.Id.menu_move:
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;
case Resource.Id.menu_copy:
var copyTask = new CopyEntry((GroupBaseActivity)Activity, App.Kp2a, (PwEntry)checkedItems.First(),
new GroupBaseActivity.RefreshTask(handler, ((GroupBaseActivity)Activity)), App.Kp2a.CurrentDb);
var copyTask = new CopyEntry(App.Kp2a, (PwEntry)checkedItems.First(),
((GroupBaseActivity)Activity).CreateRefreshAction(), App.Kp2a.CurrentDb);
ProgressTask pt = new ProgressTask(App.Kp2a, Activity, copyTask);
BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, copyTask);
pt.Run();
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)
return;
@@ -1662,30 +1701,30 @@ namespace keepass2android
}
int dbIndex = 0;
Action<bool, string, Activity> action = null;
action = (success, message, activeActivity) =>
Action<bool, string, Context> action = null;
action = (success, message, context) =>
{
if (success)
{
dbIndex++;
if (dbIndex == itemsForDatabases.Count)
{
onFinish.SetResult(true);
onFinish.Run();
onOperationFinishedHandler.SetResult(true);
onOperationFinishedHandler.Run();
return;
}
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();
}
else
{
onFinish.SetResult(false, message, true, null);
onOperationFinishedHandler.SetResult(false, message, true, null);
}
};
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();
}

View File

@@ -25,7 +25,7 @@ using Android.Runtime;
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)
{
@@ -84,12 +84,11 @@ namespace keepass2android
return baseRes;
}
public Action? OnResumeListener { get; set; }
protected override void OnResume()
{
base.OnResume();
OnResumeListener?.Invoke();
App.Kp2a.PerformPendingActions(_instanceId);
Kp2aLog.Log(ClassName + ".OnResume " + ID);
if (App.Kp2a.CurrentDb == null)
@@ -104,26 +103,38 @@ namespace keepass2android
protected override void OnStart()
{
ProgressTask.SetNewActiveActivity(this);
App.Kp2a.ActiveContext = this;
base.OnStart();
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)
{
base.OnCreate(bundle);
_instanceId = bundle?.GetInt(ID_KEY, InvalidId) ?? InvalidId;
if (_instanceId == InvalidId)
{
_instanceId = _nextContextInstanceId++;
}
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);
}
protected override void OnDestroy()
{
base.OnDestroy();
Kp2aLog.Log(ClassName + ".OnDestroy" + IsFinishing.ToString() + " " + ID);
Kp2aLog.Log(ClassName + ".OnDestroy " + IsFinishing.ToString() + " " + ID);
}
protected override void OnPause()
@@ -136,14 +147,18 @@ namespace keepass2android
{
base.OnStop();
Kp2aLog.Log(ClassName + ".OnStop" + " " + ID);
ProgressTask.RemoveActiveActivity(this);
}
protected override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
outState.PutInt(ID_KEY, _instanceId);
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 IOConnectionInfo _ioc;
private BroadcastReceiver _intentReceiver;
private BroadcastReceiver _lockCloseIntentReceiver;
private ActivityDesign _design;
public LockCloseActivity()
@@ -66,11 +66,11 @@ namespace keepass2android
if (Intent.GetBooleanExtra(NoLockCheck, false))
return;
_intentReceiver = new LockCloseActivityBroadcastReceiver(this);
_lockCloseIntentReceiver = new LockCloseActivityBroadcastReceiver(this);
IntentFilter filter = new IntentFilter();
filter.AddAction(Intents.DatabaseLocked);
filter.AddAction(Intent.ActionScreenOff);
ContextCompat.RegisterReceiver(this, _intentReceiver, filter, (int)ReceiverFlags.Exported);
ContextCompat.RegisterReceiver(this, _lockCloseIntentReceiver, filter, (int)ReceiverFlags.Exported);
}
protected override void OnDestroy()
@@ -79,7 +79,7 @@ namespace keepass2android
{
try
{
UnregisterReceiver(_intentReceiver);
UnregisterReceiver(_lockCloseIntentReceiver);
}
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.FOREGROUND_SERVICE" />
<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.MANAGE_ACCOUNTS" android:maxSdkVersion="22" />
<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.FOREGROUND_SERVICE" />
<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.MANAGE_ACCOUNTS" android:maxSdkVersion="22" />
<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_BIOMETRIC" />
<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" />

View File

@@ -222,6 +222,7 @@ namespace keepass2android
//StackBaseActivity will launch the next activity
Intent data = new Intent();
data.PutExtra("ioc", IOConnectionInfo.SerializeToString(_ioConnection));
data.PutExtra("requiresSubsequentSync", _lastLoadOperation?.RequiresSubsequentSync == true);
SetResult(Result.Ok, data);
@@ -1440,15 +1441,22 @@ namespace keepass2android
MakePasswordMaskedOrVisible();
Handler handler = new Handler();
OnFinish onFinish = new AfterLoad(handler, this, _ioConnection);
LoadDb task = (KeyProviderTypes.Contains(KeyProviders.Otp))
OnOperationFinishedHandler onOperationFinishedHandler = new AfterLoad(handler, this, _ioConnection);
LoadDb loadOperation = (KeyProviderTypes.Contains(KeyProviders.Otp))
? new SaveOtpAuxFileAndLoadDb(App.Kp2a, _ioConnection, _loadDbFileTask, compositeKey, GetKeyProviderString(),
onFinish, this, true, _makeCurrent)
: new LoadDb(this, App.Kp2a, _ioConnection, _loadDbFileTask, compositeKey, GetKeyProviderString(), onFinish,true, _makeCurrent);
onOperationFinishedHandler, this, true, _makeCurrent)
: new LoadDb(App.Kp2a, _ioConnection, _loadDbFileTask, compositeKey, GetKeyProviderString(), onOperationFinishedHandler,true, _makeCurrent);
_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)
{
Kp2aLog.LogUnexpectedError(new Exception("cannot load database: "+e + ", c: " + (compositeKey != null) + (_ioConnection != null) + (_keyFile != null), e));
@@ -1572,7 +1580,9 @@ namespace keepass2android
}
private bool hasRequestedKeyboardActivation = false;
protected override void OnStart()
private LoadDb _lastLoadOperation;
protected override void OnStart()
{
base.OnStart();
_starting = true;
@@ -1753,17 +1763,10 @@ namespace keepass2android
cbOfflineMode.Checked =
App.Kp2a
.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;
if ((cachingFileStorage != null) && cachingFileStorage.IsCached(_ioConnection))
{
offlineModeContainer.Visibility = ViewStates.Visible;
}
else
{
offlineModeContainer.Visibility = ViewStates.Gone;
App.Kp2a.OfflineMode = false;
}
CheckBox cbSyncInBackground = (CheckBox)FindViewById(Resource.Id.sync_in_background)!;
cbSyncInBackground.Checked = App.Kp2a.SyncInBackgroundPreference;
UpdateInternalCacheCheckboxesVisibility();
@@ -1891,13 +1894,14 @@ namespace keepass2android
// 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)
Handler handler = new Handler();
OnFinish onFinish = new AfterLoad(handler, this, _ioConnection);
OnOperationFinishedHandler onOperationFinishedHandler = new AfterLoad(handler, this, _ioConnection);
_performingLoad = true;
LoadDb task = new LoadDb(this, App.Kp2a, _ioConnection, _loadDbFileTask, compositeKeyForImmediateLoad, GetKeyProviderString(),
onFinish, false, _makeCurrent);
LoadDb loadOperation = new LoadDb(App.Kp2a, _ioConnection, _loadDbFileTask, compositeKeyForImmediateLoad, GetKeyProviderString(),
onOperationFinishedHandler, false, _makeCurrent);
_loadDbFileTask = null; // prevent accidental re-use
new ProgressTask(App.Kp2a, this, task).Run();
compositeKeyForImmediateLoad = null; //don't reuse or keep in memory
_lastLoadOperation = loadOperation;
OperationRunner.Instance.Run(App.Kp2a, loadOperation, true);
compositeKeyForImmediateLoad = null; //don't reuse or keep in memory
}
else
@@ -2039,10 +2043,38 @@ namespace keepass2android
{
App.Kp2a.OfflineModePreference = App.Kp2a.OfflineMode = args.IsChecked;
};
}
private String LoadKeyProviderStringForIoc(String filename) {
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) {
if ( _rememberKeyfile ) {
string keyfile = App.Kp2a.FileDbHelper.GetKeyFileForFile(filename);
if (String.IsNullOrEmpty(keyfile))
@@ -2110,11 +2142,11 @@ namespace keepass2android
Finish();
}
private class AfterLoad : OnFinish {
private class AfterLoad : OnOperationFinishedHandler {
readonly PasswordActivity _act;
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;
_ioConnection = ioConnection;
@@ -2221,7 +2253,7 @@ namespace keepass2android
if (!Success)
_act.InitFingerprintUnlock();
_act._lastLoadOperation = null;
_act._performingLoad = false;
}
@@ -2255,7 +2287,7 @@ namespace keepass2android
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;
}

View File

@@ -339,10 +339,10 @@ namespace keepass2android
if (PreferenceManager.GetDefaultSharedPreferences(this)
.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"
android:layout_marginRight="0dip" />
</com.google.android.material.textfield.TextInputLayout>
>
<CheckBox
android:id="@+id/protection"

View File

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

View File

@@ -4,18 +4,35 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
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
android:id="@+id/entry_scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:background="?android:attr/colorBackground"
android:scrollbarStyle="insideOverlay">
<keepass2android.view.EntryContentsView
android:id="@+id/entry_contents"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</ScrollView>
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/entry_edit"
android:layout_width="wrap_content"

View File

@@ -354,13 +354,22 @@
android:layout_height="1dp"
android:layout_above="@id/bottom_bar"
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
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/divider2"
android:layout_below="@id/top"
android:layout_below="@id/background_ops_container"
android:fitsSystemWindows="true">
<fragment
android:name="keepass2android.GroupListFragment"

View File

@@ -318,6 +318,30 @@
android:text="@string/help_quickunlock"
/>
</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
android:id="@+id/work_offline_container"
android:orientation="horizontal"
@@ -328,7 +352,7 @@
android:id="@+id/work_offline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/UseOfflineMode" />
<keepass2android.views.Kp2aShortHelpView
android:layout_width="wrap_content"
@@ -341,6 +365,7 @@
/>
</LinearLayout>
<Button
android:id="@+id/kill_app"
android:text="@string/kill_app_label"

View File

@@ -137,6 +137,7 @@
<string name="NoDonationReminder_key">NoDonationReminder</string>
<string name="UseOfflineCache_key">UseOfflineCache</string>
<string name="SyncOfflineCacheInBackground_key">SyncOfflineCacheInBackground_key</string>
<string name="CreateBackups_key">CreateBackups_key</string>
<string name="AcceptAllServerCertificates_key">AcceptAllServerCertificates</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="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="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_summary">Create a local backup copy after successfully loading a database.</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="EntryChannel_name">Entry notifications</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="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_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="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>

View File

@@ -12,7 +12,17 @@
android:title="@string/UseOfflineCache_title"
android:key="@string/UseOfflineCache_key" />
<CheckBoxPreference
<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
android:enabled="true"
android:persistent="true"
android:summary="@string/CreateBackups_summary"

View File

@@ -558,6 +558,7 @@ namespace keepass2android
private OpenDatabaseAdapter _adapter;
private MyBroadcastReceiver _intentReceiver;
private bool _isForeground;
private readonly List<IOConnectionInfo> _pendingBackgroundSyncs = new List<IOConnectionInfo>();
public override void OnBackPressed()
{
@@ -598,11 +599,36 @@ namespace keepass2android
string iocString = data?.GetStringExtra("ioc");
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 (OpenAutoExecEntries(App.Kp2a.CurrentDb)) return;
LaunchingOther = 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);
}

View File

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

View File

@@ -78,7 +78,15 @@ namespace keepass2android
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(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);
//if user presses back to leave this activity:
SetResult(Result.Canceled);
@@ -288,5 +296,12 @@ namespace keepass2android
{
get { return null; }
}
}}
protected override void OnDestroy()
{
base.OnDestroy();
App.Kp2a.DatabasesBackgroundModificationLock.ExitReadLock();
}
}
}

View File

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

View File

@@ -46,7 +46,10 @@ namespace keepass2android.Utils
{
public void ShowMessage(Message message)
{
Toast.MakeText(App.Context, message.Text, ToastLength.Long).Show();
new Handler(Looper.MainLooper).Post(() =>
{
Toast.MakeText(App.Context, message.Text, ToastLength.Long).Show();
});
}
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.Collections.Generic;
using System.ComponentModel.Design;
using System.IO;
using System.Linq;
using System.Net.Security;
@@ -46,6 +47,7 @@ using keepass2android;
using keepass2android.Utils;
using KeePassLib.Interfaces;
using KeePassLib.Utility;
using AlertDialog = AndroidX.AppCompat.App.AlertDialog;
using Message = keepass2android.Utils.Message;
#if !NoNet
#if !EXCLUDE_JAVAFILESTORAGE
@@ -56,6 +58,9 @@ using GoogleDriveAppDataFileStorage = keepass2android.Io.GoogleDriveAppDataFileS
using PCloudFileStorage = keepass2android.Io.PCloudFileStorage;
using static keepass2android.Util;
using static Android.Provider.Telephony.MmsSms;
using AndroidX.Lifecycle;
using Java.Interop;
using keepass2android.services;
#endif
#endif
@@ -111,15 +116,12 @@ namespace keepass2android
#endif
/// <summary>
/// Main implementation of the IKp2aApp interface for usage in the real app.
/// </summary>
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())
{
@@ -147,10 +149,13 @@ namespace keepass2android
// Couldn't quick-lock, so unload database(s) instead
_openAttempts.Clear();
_openDatabases.Clear();
_currentDatabase = null;
LastOpenedEntry = null;
QuickLocked = false;
_openDatabases.Clear();
_currentDatabase = null;
LastOpenedEntry = null;
QuickLocked = false;
}
}
else
@@ -186,42 +191,73 @@ 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 createBackup = prefs.GetBoolean(LocaleManager.LocalizedAppContext.GetString(Resource.String.CreateBackups_key), true)
var prefs = PreferenceManager.GetDefaultSharedPreferences(LocaleManager.LocalizedAppContext);
var createBackup =
prefs.GetBoolean(LocaleManager.LocalizedAppContext.GetString(Resource.String.CreateBackups_key),
true)
&& !(new LocalFileStorage(this).IsLocalBackup(ioConnectionInfo));
MemoryStream backupCopy = new MemoryStream();
if (createBackup)
{
MemoryStream backupCopy = new MemoryStream();
if (createBackup)
{
memoryStream.CopyTo(backupCopy);
backupCopy.Seek(0, SeekOrigin.Begin);
//reset stream if we need to reuse it later:
memoryStream.Seek(0, SeekOrigin.Begin);
}
memoryStream.CopyTo(backupCopy);
backupCopy.Seek(0, SeekOrigin.Begin);
//reset stream if we need to reuse it later:
memoryStream.Seek(0, SeekOrigin.Begin);
}
foreach (Database openDb in _openDatabases)
{
if (openDb.Ioc.IsSameFileAs(ioConnectionInfo))
{
//TODO check this earlier and simply open the database's root group
throw new Exception("Database already loaded!");
}
}
if (!statusLogger.ContinueWork())
{
throw new Java.Lang.InterruptedException();
}
_openAttempts.Add(ioConnectionInfo);
var newDb = new Database(new DrawableFactory(), this);
_openAttempts.Add(ioConnectionInfo);
var newDb = new Database(new DrawableFactory(), this);
newDb.LoadData(this, ioConnectionInfo, memoryStream, compositeKey, statusLogger, databaseFormat);
modificationWatcher.BeforeModifyDatabases();
try
{
if ((_currentDatabase == null) || makeCurrent)
_currentDatabase = newDb;
_openDatabases.Add(newDb);
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;
}
replacedOpenDatabase = true;
_openDatabases[i] = newDb;
break;
}
}
if (!replacedOpenDatabase)
{
_openDatabases.Add(newDb);
}
}
finally
{
modificationWatcher.AfterModifyDatabases();
}
if (createBackup)
@@ -276,25 +312,28 @@ namespace keepass2android
return newDb;
}
public void CloseDatabase(Database db)
public void CloseDatabase(Database db)
{
if (!_openDatabases.Contains(db))
throw new Exception("Cannot close database which is not open!");
if (_openDatabases.Count == 1)
{
Lock(false);
return;
}
if (LastOpenedEntry != null && db.EntriesById.ContainsKey(LastOpenedEntry.Uuid))
{
LastOpenedEntry = null;
}
_openDatabases.Remove(db);
if (_currentDatabase == db)
_currentDatabase = _openDatabases.First();
if (!_openDatabases.Contains(db))
throw new Exception("Cannot close database which is not open!");
if (_openDatabases.Count == 1)
{
Lock(false);
return;
}
if (LastOpenedEntry != null && db.EntriesById.ContainsKey(LastOpenedEntry.Uuid))
{
LastOpenedEntry = null;
}
_openDatabases.Remove(db);
if (_currentDatabase == db)
_currentDatabase = _openDatabases.First();
UpdateOngoingNotification();
//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<Database> _openDatabases = new List<Database>();
private readonly List<IOConnectionInfo> _childDatabases = new List<IOConnectionInfo>(); //list of databases which were opened as child databases
private Database _currentDatabase;
public IEnumerable<Database> OpenDatabases
{
get { return _openDatabases; }
get
{
return _openDatabases;
}
}
internal ChallengeXCKey _currentlyWaitingXcKey;
@@ -410,8 +453,9 @@ namespace keepass2android
DirtyGroups.Add(group);
}
}
var intent = new Intent(Intents.DataUpdated);
App.Context.SendBroadcast(intent);
}
/// <summary>
@@ -477,6 +521,8 @@ namespace keepass2android
// Whether the app is currently showing a dialog that requires user input, like a yesNoCancel dialog
private bool _isShowingUserInputDialog = false;
private IMessagePresenter? _messagePresenter;
private YesNoCancelQuestion? _currentlyPendingYesNoCancelQuestion = null;
private Context? _activeContext;
private void AskForReload(Activity activity, Action<bool> actionOnResult)
{
@@ -573,123 +619,194 @@ namespace keepass2android
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,
yesHandler, noHandler, cancelHandler, ctx, messageSuffix);
yesHandler, noHandler, cancelHandler, messageSuffix);
}
public void AskYesNoCancel(UiStringKey titleKey, UiStringKey messageKey,
UiStringKey yesString, UiStringKey noString,
EventHandler<DialogClickEventArgs> yesHandler,
EventHandler<DialogClickEventArgs> noHandler,
EventHandler<DialogClickEventArgs> cancelHandler,
Context ctx, string messageSuffix = "")
EventHandler<DialogClickEventArgs> cancelHandler, 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
{
private AlertDialog? _dialog;
public UiStringKey TitleKey { get; set; }
public UiStringKey MessageKey { get; set; }
public UiStringKey YesString { get; set; }
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; }
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
// and to show progress dialog after yesNoCancel dialog is closed
EventHandler<DialogClickEventArgs> yesHandlerWithShow = (sender, args) =>
{
onUserInputDialogClose();
YesHandler.Invoke(sender, args);
};
string yesText = app.GetResourceString(YesString);
builder.SetPositiveButton(yesText, yesHandlerWithShow);
string noText = "";
if (NoHandler != null)
{
EventHandler<DialogClickEventArgs> noHandlerWithShow = (sender, args) =>
{
onUserInputDialogClose();
NoHandler.Invoke(sender, args);
};
noText = app.GetResourceString(NoString);
builder.SetNegativeButton(noText, noHandlerWithShow);
}
string cancelText = "";
if (CancelHandler != null)
{
EventHandler<DialogClickEventArgs> cancelHandlerWithShow = (sender, args) =>
{
onUserInputDialogClose();
CancelHandler.Invoke(sender, args);
};
cancelText = App.Context.GetString(Android.Resource.String.Cancel);
builder.SetNeutralButton(cancelText,
cancelHandlerWithShow);
}
_dialog = builder.Create();
if (DismissHandler != null)
{
_dialog.SetOnDismissListener(new Util.DismissListener(() =>
{
onUserInputDialogClose();
DismissHandler(_dialog, EventArgs.Empty);
}));
}
else
{
_dialog.SetCancelable(false);
}
onUserInputDialogShow();
try
{
_dialog.Show();
}
catch (Exception e)
{
Kp2aLog.LogUnexpectedError(e);
}
if (yesText.Length + noText.Length + cancelText.Length >= 20)
{
try
{
Button button = _dialog.GetButton((int)DialogButtonType.Positive);
LinearLayout linearLayout = (LinearLayout)button.Parent;
linearLayout.Orientation = Orientation.Vertical;
}
catch (Exception e)
{
Kp2aLog.LogUnexpectedError(e);
}
}
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,
Context ctx, string messageSuffix = "")
EventHandler dismissHandler,string messageSuffix = "")
{
Handler handler = new Handler(Looper.MainLooper);
handler.Post(() =>
{
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(ctx);
builder.SetTitle(GetResourceString(titleKey));
builder.SetMessage(GetResourceString(messageKey) + (messageSuffix != "" ? " " + messageSuffix : ""));
// _____handlerWithShow are wrappers around given handlers to update _isSHowingYesNoCancelDialog
// and to show progress dialog after yesNoCancel dialog is closed
EventHandler<DialogClickEventArgs> yesHandlerWithShow = (sender, args) =>
{
OnUserInputDialogClose();
yesHandler.Invoke(sender, args);
};
string yesText = GetResourceString(yesString);
builder.SetPositiveButton(yesText, yesHandlerWithShow);
string noText = "";
if (noHandler != null)
{
EventHandler<DialogClickEventArgs> noHandlerWithShow = (sender, args) =>
{
OnUserInputDialogClose();
noHandler.Invoke(sender, args);
};
noText = GetResourceString(noString);
builder.SetNegativeButton(noText, noHandlerWithShow);
}
string cancelText = "";
if (cancelHandler != null)
{
EventHandler<DialogClickEventArgs> cancelHandlerWithShow = (sender, args) =>
{
OnUserInputDialogClose();
cancelHandler.Invoke(sender, args);
};
cancelText = ctx.GetString(Android.Resource.String.Cancel);
builder.SetNeutralButton(cancelText,
cancelHandlerWithShow);
}
var dialog = builder.Create();
if (dismissHandler != null)
{
dialog.SetOnDismissListener(new Util.DismissListener(() => {
OnUserInputDialogClose();
dismissHandler(dialog, EventArgs.Empty);
}));
}
OnUserInputDialogShow();
dialog.Show();
if (yesText.Length + noText.Length + cancelText.Length >= 20)
{
try
{
Button button = dialog.GetButton((int)DialogButtonType.Positive);
LinearLayout linearLayout = (LinearLayout)button.Parent;
linearLayout.Orientation = Orientation.Vertical;
}
catch (Exception e)
{
Kp2aLog.LogUnexpectedError(e);
}
}
}
);
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>
/// Shows all non-dismissed progress dialogs.
/// 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
/// because they are just progress indicators.
/// </summary>
private void ShowAllActiveProgressDialogs()
{
{
foreach (RealProgressDialog progressDialog in _activeProgressDialogs)
{
progressDialog.Show();
progressDialog.Show();
}
}
private void HideAllActiveProgressDialogs()
{
foreach (RealProgressDialog progressDialog in _activeProgressDialogs)
foreach (RealProgressDialog progressDialog in _activeProgressDialogs)
{
progressDialog.Hide();
progressDialog.Hide();
}
}
@@ -709,13 +826,15 @@ namespace keepass2android
private void OnUserInputDialogClose()
{
_isShowingUserInputDialog = false;
ShowAllActiveProgressDialogs();
_currentlyPendingYesNoCancelQuestion = null;
ShowAllActiveProgressDialogs();
}
public Handler UiThreadHandler
{
get { return new Handler(); }
}
public Handler UiThreadHandler
{
get { return _uiThreadHandler; }
}
/// <summary>
/// Simple wrapper around ProgressDialog implementing IProgressDialog
@@ -760,8 +879,15 @@ namespace keepass2android
_app._activeProgressDialogs.Add(this);
// Only show if asking dialog not also showing
if (!_app._isShowingUserInputDialog)
{
_pd.Show();
{
try
{
_pd.Show();
}
catch (Exception e)
{
Kp2aLog.LogUnexpectedError(e);
}
}
}
@@ -771,10 +897,25 @@ namespace keepass2android
}
}
public IProgressDialog CreateProgressDialog(Context ctx)
{
return new RealProgressDialog(ctx, this);
}
public IProgressDialog CreateProgressDialog(Context ctx)
{
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)
{
@@ -798,9 +939,14 @@ namespace keepass2android
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;
}
@@ -1012,6 +1158,8 @@ namespace keepass2android
FileDbHelper = new FileDbHelper(app);
FileDbHelper.Open();
_uiThreadHandler = new Handler(Looper.MainLooper);
#if DEBUG
foreach (UiStringKey key in Enum.GetValues(typeof(UiStringKey)))
{
@@ -1151,10 +1299,35 @@ namespace keepass2android
}
}
/// <summary>
/// true if the app is used in offline mode
/// </summary>
public bool OfflineMode
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>
/// true if the app is used in offline mode
/// </summary>
public bool OfflineMode
{
get; set;
}
@@ -1250,13 +1423,22 @@ namespace keepass2android
public Database TryFindDatabaseForElement(IStructureItem element)
{
foreach (var db in OpenDatabases)
try
{
//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))
return db;
foreach (var db in OpenDatabases)
{
//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))
{
return db;
}
}
}
return null;
catch (Exception e)
{
Kp2aLog.LogUnexpectedError(e);
}
return null;
}
public void RegisterChildDatabase(IOConnectionInfo ioc)
@@ -1329,10 +1511,92 @@ 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.
#if NoNet
[Application(Debuggable=false, Label=AppNames.AppName)]
#else
@@ -1342,7 +1606,8 @@ namespace keepass2android
[Application(Debuggable = true, Label = AppNames.AppName)]
#endif
#endif
public class App : Application {
public class App : Application, ILifecycleObserver
{
public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
{
@@ -1375,8 +1640,9 @@ namespace keepass2android
InitThaiCalendarCrashFix();
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);
CreateNotificationChannels();
@@ -1388,12 +1654,33 @@ namespace keepass2android
intentFilter.AddAction(Intents.LockDatabaseByTimeout);
intentFilter.AddAction(Intents.CloseDatabase);
ContextCompat.RegisterReceiver(Context, broadcastReceiver, intentFilter, (int)ReceiverFlags.Exported);
//ZXing.Net.Mobile.Forms.Android.Platform.Init();
}
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()
{

View File

@@ -37,8 +37,13 @@ namespace keepass2android
/// <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";
/// <summary>This intent will be broadcast once the keyboard data has been cleared</summary>
public const String KeyboardCleared = "keepass2android." + AppNames.PackagePart + ".keyboard_cleared";
/// <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>
public const String KeyboardCleared = "keepass2android." + AppNames.PackagePart + ".keyboard_cleared";
public const String CopyUsername = "keepass2android.copy_username";
public const String CopyPassword = "keepass2android.copy_password";

View File

@@ -48,7 +48,15 @@ namespace keepass2android.search
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (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);
if ( IsFinishing ) {
return;
@@ -59,7 +67,13 @@ namespace keepass2android.search
ProcessIntent(Intent);
}
public override bool EntriesBelongToCurrentDatabaseOnly
protected override void OnDestroy()
{
base.OnDestroy();
App.Kp2a.DatabasesBackgroundModificationLock.ExitReadLock();
}
public override bool EntriesBelongToCurrentDatabaseOnly
{
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;
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)
{
db.KpDatabase.DefaultUserName = previousUsername;
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();
};
}
@@ -144,8 +144,8 @@ namespace keepass2android
{
pref.PreferenceClick += (sender, args) =>
{
ProgressTask pt = new ProgressTask(App.Kp2a, Activity,
new AddTemplateEntries(Activity, App.Kp2a, new ActionOnFinish(Activity,
BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a,
new AddTemplateEntries(App.Kp2a, new ActionOnOperationFinished(App.Kp2a,
delegate
{
pref.Enabled = false;
@@ -183,13 +183,13 @@ namespace keepass2android
String previousName = db.KpDatabase.Name;
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)
{
db.KpDatabase.Name = previousName;
db.KpDatabase.NameChanged = previousNameChanged;
App.Kp2a.ShowMessage(activity, message, MessageSeverity.Error);
App.Kp2a.ShowMessage(context, message, MessageSeverity.Error);
}
else
{
@@ -197,7 +197,7 @@ namespace keepass2android
App.Kp2a.UpdateOngoingNotification();
}
}));
ProgressTask pt = new ProgressTask(App.Kp2a, Activity, save);
BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, save);
pt.Run();
};
}
@@ -410,18 +410,18 @@ namespace keepass2android
var previousCipher = db.KpDatabase.DataCipherUuid;
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)
{
db.KpDatabase.DataCipherUuid = previousCipher;
App.Kp2a.ShowMessage(activity, message, MessageSeverity.Error);
App.Kp2a.ShowMessage(context, message, MessageSeverity.Error);
return;
}
preferenceChangeEventArgs.Preference.Summary =
CipherPool.GlobalPool.GetCipher(db.KpDatabase.DataCipherUuid).DisplayName;
}));
ProgressTask pt = new ProgressTask(App.Kp2a, Activity, save);
BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, save);
pt.Run();
}
@@ -1071,18 +1071,18 @@ namespace keepass2android
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)
{
db.KpDatabase.KdfParameters = previousKdfParams;
App.Kp2a.ShowMessage(activity, message, MessageSeverity.Error);
App.Kp2a.ShowMessage(context, message, MessageSeverity.Error);
return;
}
UpdateKdfScreen();
}));
ProgressTask pt = new ProgressTask(App.Kp2a, Activity, save);
BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, save);
pt.Run();
}

View File

@@ -13,13 +13,13 @@ namespace keepass2android
public class ExportKeyfileActivity : LockCloseActivity
{
public class ExportKeyfile : RunnableOnFinish
public class ExportKeyfile : OperationWithFinishHandler
{
private readonly IKp2aApp _app;
private IOConnectionInfo _targetIoc;
public ExportKeyfile(Activity activity, IKp2aApp app, OnFinish onFinish, IOConnectionInfo targetIoc) : base(
activity, onFinish)
public ExportKeyfile(IKp2aApp app, OnOperationFinishedHandler onOperationFinishedHandler, IOConnectionInfo targetIoc) : base(
App.Kp2a, onOperationFinishedHandler)
{
_app = app;
_targetIoc = targetIoc;
@@ -63,25 +63,25 @@ namespace keepass2android
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)
{
var exportKeyfile = new ExportKeyfile(_activity, App.Kp2a, new ActionOnFinish(_activity,
(success, message, activity) =>
var exportKeyfile = new ExportKeyfile(App.Kp2a, new ActionInContextInstanceOnOperationFinished(_activity.ContextInstanceId, App.Kp2a,
(success, message, context) =>
{
if (!success)
App.Kp2a.ShowMessage(activity, message, MessageSeverity.Error);
App.Kp2a.ShowMessage(context, message, MessageSeverity.Error);
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);
activity.Finish();
(context as Activity)?.Finish();
}
), ioc);
ProgressTask pt = new ProgressTask(App.Kp2a, _activity, exportKeyfile);
BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, exportKeyfile);
pt.Run();
}

View File

@@ -113,8 +113,8 @@ namespace keepass2android.settings
ParamValue = paramValue;
Handler handler = new Handler();
SaveDb save = new SaveDb((Activity)Context, App.Kp2a, App.Kp2a.CurrentDb, new AfterSave((Activity)Context, handler, oldValue, this));
ProgressTask pt = new ProgressTask(App.Kp2a, (Activity)Context, save);
SaveDb save = new SaveDb(App.Kp2a, App.Kp2a.CurrentDb, new AfterSave(App.Kp2a, handler, oldValue, this));
BlockingOperationStarter pt = new BlockingOperationStarter(App.Kp2a, save);
pt.Run();
});
db.SetNegativeButton(Android.Resource.String.Cancel, ((sender, args) => { }));
@@ -129,15 +129,14 @@ namespace keepass2android.settings
public abstract ulong ParamValue { get; set; }
private class AfterSave : OnFinish {
private class AfterSave : OnOperationFinishedHandler {
private readonly ulong _oldParamValue;
private readonly Context _ctx;
private readonly KdfNumberDialogPreference _pref;
private readonly IKp2aApp _app;
private readonly KdfNumberDialogPreference _pref;
public AfterSave(Activity ctx, Handler handler, ulong oldParamValue, KdfNumberDialogPreference pref):base(ctx, handler) {
_pref = pref;
_ctx = ctx;
public AfterSave(IKp2aApp app, Handler handler, ulong oldParamValue, KdfNumberDialogPreference pref):base(app, handler) {
_app = app;
_pref = pref;
_oldParamValue = oldParamValue;
}
@@ -148,7 +147,7 @@ namespace keepass2android.settings
_pref.OnPreferenceChangeListener.OnPreferenceChange(_pref, null);
}
} else {
DisplayMessage(_ctx);
DisplayMessage(_app.ActiveContext);
_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.Res;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Text;
using Android.Text.Method;
@@ -18,6 +17,7 @@ using Android.Views;
using Android.Widget;
using Google.Android.Material.Dialog;
using keepass2android;
using KeePassLib.Interfaces;
namespace keepass2android.views
{

View File

@@ -125,94 +125,103 @@ namespace keepass2android.view
if (_groupBaseActivity.IsFinishing)
return;
_entry = pw;
_pos = pos;
ev.FindViewById(Resource.Id.icon).Visibility = ViewStates.Visible;
ev.FindViewById(Resource.Id.check_mark).Visibility = ViewStates.Invisible;
_pos = pos;
try
{
_db = App.Kp2a.FindDatabaseForElement(_entry);
ImageView iv = (ImageView)ev.FindViewById(Resource.Id.icon);
bool isExpired = pw.Expires && pw.ExpiryTime < DateTime.Now;
if (isExpired)
{
_db.DrawableFactory.AssignDrawableTo(iv, Context, _db.KpDatabase, PwIcon.Expired, PwUuid.Zero, false);
} else
{
_db.DrawableFactory.AssignDrawableTo(iv, Context, _db.KpDatabase, pw.IconId, pw.CustomIconUuid, false);
}
String title = pw.Strings.ReadSafe(PwDefs.TitleField);
title = SprEngine.Compile(title, new SprContext(_entry, _db.KpDatabase, SprCompileFlags.All));
var str = new SpannableString(title);
if (isExpired)
{
str.SetSpan(new StrikethroughSpan(), 0, title.Length, SpanTypes.ExclusiveExclusive);
}
_textView.TextFormatted = str;
if (_defaultTextColor == null)
_defaultTextColor = _textView.TextColors.DefaultColor;
if (_groupActivity.IsBeingMoved(_entry.Uuid))
{
int elementBeingMoved = Context.Resources.GetColor(Resource.Color.md_theme_inversePrimary);
_textView.SetTextColor(new Color(elementBeingMoved));
}
else
_textView.SetTextColor(new Color((int)_defaultTextColor));
String detail = pw.Strings.ReadSafe(PwDefs.UserNameField);
detail = SprEngine.Compile(detail, new SprContext(_entry, _db.KpDatabase, SprCompileFlags.All));
if ((_showDetail == false) || (String.IsNullOrEmpty(detail)))
{
_textviewDetails.Visibility = ViewStates.Gone;
}
else
{
var strDetail = new SpannableString(detail);
if (isExpired)
{
strDetail.SetSpan(new StrikethroughSpan(), 0, detail.Length, SpanTypes.ExclusiveExclusive);
}
_textviewDetails.TextFormatted = strDetail;
_textviewDetails.Visibility = ViewStates.Visible;
}
if ( (!_showGroupFullPath) || (!_isSearchResult) ) {
_textgroupFullPath.Visibility = ViewStates.Gone;
}
else {
String groupDetail = pw.ParentGroup.GetFullPath();
if (App.Kp2a.OpenDatabases.Count() > 1)
{
groupDetail += "(" + App.Kp2a.GetFileStorage(_db.Ioc).GetDisplayName(_db.Ioc) + ")";
}
var strGroupDetail = new SpannableString (groupDetail);
if (isExpired) {
strGroupDetail.SetSpan (new StrikethroughSpan (), 0, groupDetail.Length, SpanTypes.ExclusiveExclusive);
}
_textgroupFullPath.TextFormatted = strGroupDetail;
_textgroupFullPath.Visibility = ViewStates.Visible;
}
//try to get totp data
UpdateTotp();
ev.FindViewById(Resource.Id.icon).Visibility = ViewStates.Visible;
ev.FindViewById(Resource.Id.check_mark).Visibility = ViewStates.Invisible;
_db = App.Kp2a.FindDatabaseForElement(_entry);
ImageView iv = (ImageView)ev.FindViewById(Resource.Id.icon);
bool isExpired = pw.Expires && pw.ExpiryTime < DateTime.Now;
if (isExpired)
{
_db.DrawableFactory.AssignDrawableTo(iv, Context, _db.KpDatabase, PwIcon.Expired, PwUuid.Zero, false);
}
else
{
_db.DrawableFactory.AssignDrawableTo(iv, Context, _db.KpDatabase, pw.IconId, pw.CustomIconUuid, false);
}
String title = pw.Strings.ReadSafe(PwDefs.TitleField);
title = SprEngine.Compile(title, new SprContext(_entry, _db.KpDatabase, SprCompileFlags.All));
var str = new SpannableString(title);
if (isExpired)
{
str.SetSpan(new StrikethroughSpan(), 0, title.Length, SpanTypes.ExclusiveExclusive);
}
_textView.TextFormatted = str;
if (_defaultTextColor == null)
_defaultTextColor = _textView.TextColors.DefaultColor;
if (_groupActivity.IsBeingMoved(_entry.Uuid))
{
int elementBeingMoved = Context.Resources.GetColor(Resource.Color.md_theme_inversePrimary);
_textView.SetTextColor(new Color(elementBeingMoved));
}
else
_textView.SetTextColor(new Color((int)_defaultTextColor));
String detail = pw.Strings.ReadSafe(PwDefs.UserNameField);
detail = SprEngine.Compile(detail, new SprContext(_entry, _db.KpDatabase, SprCompileFlags.All));
if ((_showDetail == false) || (String.IsNullOrEmpty(detail)))
{
_textviewDetails.Visibility = ViewStates.Gone;
}
else
{
var strDetail = new SpannableString(detail);
if (isExpired)
{
strDetail.SetSpan(new StrikethroughSpan(), 0, detail.Length, SpanTypes.ExclusiveExclusive);
}
_textviewDetails.TextFormatted = strDetail;
_textviewDetails.Visibility = ViewStates.Visible;
}
if ((!_showGroupFullPath) || (!_isSearchResult))
{
_textgroupFullPath.Visibility = ViewStates.Gone;
}
else
{
String groupDetail = pw.ParentGroup.GetFullPath();
if (App.Kp2a.OpenDatabases.Count() > 1)
{
groupDetail += "(" + App.Kp2a.GetFileStorage(_db.Ioc).GetDisplayName(_db.Ioc) + ")";
}
var strGroupDetail = new SpannableString(groupDetail);
if (isExpired)
{
strGroupDetail.SetSpan(new StrikethroughSpan(), 0, groupDetail.Length, SpanTypes.ExclusiveExclusive);
}
_textgroupFullPath.TextFormatted = strGroupDetail;
_textgroupFullPath.Visibility = ViewStates.Visible;
}
//try to get totp data
UpdateTotp();
}
catch (Exception e)
{
Kp2aLog.LogUnexpectedError(e);
}
}