More refactoring and introduction of further abstractions

Added further tests
Fixed test loading with keyfile only
This commit is contained in:
Philipp Crocoll
2013-06-25 21:27:41 +02:00
parent 0435ad54ca
commit 903de8368a
22 changed files with 456 additions and 56 deletions

View File

@@ -1,5 +1,6 @@
using System;
using Android.Content;
using Android.OS;
using KeePassLib.Serialization;
namespace keepass2android
@@ -49,5 +50,12 @@ namespace keepass2android
EventHandler<DialogClickEventArgs> noHandler,
EventHandler<DialogClickEventArgs> cancelHandler,
Context ctx);
/// <summary>
/// Returns a Handler object which can run tasks on the UI thread
/// </summary>
Handler UiThreadHandler { get; }
IProgressDialog CreateProgressDialog(Context ctx);
}
}

View File

@@ -0,0 +1,10 @@
namespace keepass2android
{
public interface IProgressDialog
{
void SetTitle(string title);
void SetMessage(string getResourceString);
void Dismiss();
void Show();
}
}

View File

@@ -41,6 +41,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="IProgressDialog.cs" />
<Compile Include="PreferenceKey.cs" />
<Compile Include="UiStringKey.cs" />
<Compile Include="database\Database.cs" />

View File

@@ -28,16 +28,17 @@ namespace keepass2android
public class ProgressTask {
private readonly Handler _handler;
private readonly RunnableOnFinish _task;
private readonly ProgressDialog _progressDialog;
private readonly IProgressDialog _progressDialog;
private readonly IKp2aApp _app;
private Thread _thread;
public ProgressTask(IKp2aApp app, Context ctx, RunnableOnFinish task, UiStringKey messageKey) {
_task = task;
_handler = new Handler();
_handler = app.UiThreadHandler;
_app = app;
// Show process dialog
_progressDialog = new ProgressDialog(ctx);
_progressDialog = app.CreateProgressDialog(ctx);
_progressDialog.SetTitle(_app.GetResourceString(UiStringKey.progress_title));
_progressDialog.SetMessage(_app.GetResourceString(messageKey));
@@ -53,25 +54,37 @@ namespace keepass2android
// Start Thread to Run task
Thread t = new Thread(_task.Run);
t.Start();
_thread = new Thread(_task.Run);
_thread.Start();
}
public void JoinWorkerThread()
{
_thread.Join();
}
private class AfterTask : OnFinish {
readonly ProgressDialog _progressDialog;
readonly IProgressDialog _progressDialog;
public AfterTask (OnFinish finish, Handler handler, ProgressDialog pd): base(finish, handler)
public AfterTask (OnFinish finish, Handler handler, IProgressDialog pd): base(finish, handler)
{
_progressDialog = pd;
}
public override void Run() {
base.Run();
// Remove the progress dialog
Handler.Post(delegate {_progressDialog.Dismiss();});
if (Handler != null) //can be null in tests
{
// Remove the progress dialog
Handler.Post(delegate { _progressDialog.Dismiss(); });
}
else
{
_progressDialog.Dismiss();
}
}
}

View File

@@ -26,7 +26,7 @@ namespace keepass2android
/// StatusLogger implementation which shows the progress in a progress dialog
/// </summary>
public class UpdateStatus: IStatusLogger {
private readonly ProgressDialog _progressDialog;
private readonly IProgressDialog _progressDialog;
readonly IKp2aApp _app;
private readonly Handler _handler;
@@ -34,7 +34,7 @@ namespace keepass2android
}
public UpdateStatus(IKp2aApp app, Handler handler, ProgressDialog pd) {
public UpdateStatus(IKp2aApp app, Handler handler, IProgressDialog pd) {
_app = app;
_progressDialog = pd;
_handler = handler;

View File

@@ -19,6 +19,7 @@ using System;
using System.Collections.Generic;
using Android.Content;
using KeePassLib;
using KeePassLib.Keys;
using KeePassLib.Serialization;
namespace keepass2android
@@ -95,21 +96,36 @@ namespace keepass2android
PwDatabase pwDatabase = new PwDatabase();
KeePassLib.Keys.CompositeKey key = new KeePassLib.Keys.CompositeKey();
key.AddUserKey(new KeePassLib.Keys.KcpPassword(password));
CompositeKey compositeKey = new CompositeKey();
compositeKey.AddUserKey(new KcpPassword(password));
if (!String.IsNullOrEmpty(keyfile))
{
try
{
key.AddUserKey(new KeePassLib.Keys.KcpKeyFile(keyfile));
compositeKey.AddUserKey(new KcpKeyFile(keyfile));
} catch (Exception)
{
throw new KeyFileException();
}
}
pwDatabase.Open(iocInfo, key, status);
try
{
pwDatabase.Open(iocInfo, compositeKey, status);
}
catch (Exception)
{
if ((password == "") && (keyfile != null))
{
//if we don't get a password, we don't know whether this means "empty password" or "no password"
//retry without password:
compositeKey.RemoveUserKey(compositeKey.GetUserKey(typeof (KcpPassword)));
pwDatabase.Open(iocInfo, compositeKey, status);
}
else throw;
}
if (iocInfo.IsLocalFile())
{

View File

@@ -41,15 +41,17 @@ namespace keepass2android
public override void Run ()
{
try {
try
{
_app.GetDb().LoadData (_app, _ioc, _pass, _key, Status);
SaveFileData (_ioc, _key);
} catch (KeyFileException) {
Android.Util.Log.Debug("KP2ATest", "KeyFileException");
Finish(false, /*TODO Localize: use Keepass error text KPRes.KeyFileError (including "or invalid format")*/ _app.GetResourceString(UiStringKey.keyfile_does_not_exist));
}
catch (Exception e) {
Android.Util.Log.Debug("KP2ATest", "Exception: "+e.Message);
Finish(false, "An error occured: " + e.Message);
return;
}
@@ -85,6 +87,7 @@ namespace keepass2android
return;
}
*/
Android.Util.Log.Debug("KP2ATest", "LoadDB OK");
Finish(true);
}

View File

@@ -58,15 +58,14 @@ namespace keepass2android
}
public virtual void Run() {
if ( BaseOnFinish != null ) {
// Pass on result on call finish
BaseOnFinish.SetResult(Success, Message);
if (BaseOnFinish == null) return;
// Pass on result on call finish
BaseOnFinish.SetResult(Success, Message);
if ( Handler != null ) {
Handler.Post(BaseOnFinish.Run);
} else {
BaseOnFinish.Run();
}
if ( Handler != null ) {
Handler.Post(BaseOnFinish.Run);
} else {
BaseOnFinish.Run();
}
}