Refactoring:

Wiped out the historical partial Java naming conventions, replaced by C#
removed unused fields/parameters
removed many unused usings
...
(Thanks to ReSharper :-))
This commit is contained in:
Philipp Crocoll
2013-06-15 12:40:01 +02:00
parent 26575c4ba4
commit d2a06617eb
86 changed files with 1936 additions and 2711 deletions

View File

@@ -16,16 +16,7 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace keepass2android
{
@@ -33,34 +24,34 @@ namespace keepass2android
{
public delegate void ActionToPerformOnFinsh(bool success, String message);
ActionToPerformOnFinsh actionToPerform;
readonly ActionToPerformOnFinsh _actionToPerform;
public ActionOnFinish(ActionToPerformOnFinsh actionToPerform) : base(null, null)
{
this.actionToPerform = actionToPerform;
_actionToPerform = actionToPerform;
}
public ActionOnFinish(ActionToPerformOnFinsh actionToPerform, OnFinish finish) : base(finish)
{
this.actionToPerform = actionToPerform;
_actionToPerform = actionToPerform;
}
public ActionOnFinish(ActionToPerformOnFinsh actionToPerform, Handler handler) : base(handler)
{
this.actionToPerform = actionToPerform;
_actionToPerform = actionToPerform;
}
public override void run()
public override void Run()
{
if (this.mMessage == null)
this.mMessage = "";
if (this.mHandler != null)
if (Message == null)
Message = "";
if (Handler != null)
{
this.mHandler.Post(() => {actionToPerform(this.mSuccess, this.mMessage);});
Handler.Post(() => {_actionToPerform(Success, Message);});
}
else
actionToPerform(this.mSuccess, this.mMessage);
base.run();
_actionToPerform(Success, Message);
base.Run();
}
}
}

View File

@@ -15,79 +15,69 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using KeePassLib;
namespace keepass2android
{
public class AddEntry : RunnableOnFinish {
protected Database mDb;
private PwEntry mEntry;
private PwGroup mParentGroup;
private Context mCtx;
protected Database Db;
private readonly PwEntry _entry;
private readonly PwGroup _parentGroup;
private readonly Context _ctx;
public static AddEntry getInstance(Context ctx, Database db, PwEntry entry, PwGroup parentGroup, OnFinish finish) {
public static AddEntry GetInstance(Context ctx, Database db, PwEntry entry, PwGroup parentGroup, OnFinish finish) {
return new AddEntry(ctx, db, entry, parentGroup, finish);
}
protected AddEntry(Context ctx, Database db, PwEntry entry, PwGroup parentGroup, OnFinish finish):base(finish) {
mCtx = ctx;
mParentGroup = parentGroup;
mDb = db;
mEntry = entry;
_ctx = ctx;
_parentGroup = parentGroup;
Db = db;
_entry = entry;
mFinish = new AfterAdd(db, entry, mFinish);
OnFinishToRun = new AfterAdd(db, entry, OnFinishToRun);
}
public override void run() {
mParentGroup.AddEntry(mEntry, true);
public override void Run() {
_parentGroup.AddEntry(_entry, true);
// Commit to disk
SaveDB save = new SaveDB(mCtx, mDb, mFinish);
save.run();
SaveDb save = new SaveDb(_ctx, Db, OnFinishToRun);
save.Run();
}
private class AfterAdd : OnFinish {
protected Database mDb;
private PwEntry mEntry;
private readonly Database _db;
private readonly PwEntry _entry;
public AfterAdd(Database db, PwEntry entry, OnFinish finish):base(finish) {
mDb = db;
mEntry = entry;
_db = db;
_entry = entry;
}
public override void run() {
if ( mSuccess ) {
public override void Run() {
if ( Success ) {
PwGroup parent = mEntry.ParentGroup;
PwGroup parent = _entry.ParentGroup;
// Mark parent group dirty
mDb.dirty.Add(parent);
_db.Dirty.Add(parent);
// Add entry to global
mDb.entries[mEntry.Uuid] = mEntry;
_db.Entries[_entry.Uuid] = _entry;
} else {
//TODO test fail
mEntry.ParentGroup.Entries.Remove(mEntry);
_entry.ParentGroup.Entries.Remove(_entry);
}
base.run();
base.Run();
}
}

View File

@@ -16,82 +16,71 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using KeePassLib;
namespace keepass2android
{
public class AddGroup : RunnableOnFinish {
internal Database mDb;
private String mName;
private int mIconID;
internal PwGroup mGroup;
internal PwGroup mParent;
protected bool mDontSave;
Context mCtx;
internal Database Db;
private readonly String _name;
private readonly int _iconId;
internal PwGroup Group;
internal PwGroup Parent;
protected bool DontSave;
readonly Context _ctx;
public static AddGroup getInstance(Context ctx, Database db, String name, int iconid, PwGroup parent, OnFinish finish, bool dontSave) {
public static AddGroup GetInstance(Context ctx, Database db, String name, int iconid, PwGroup parent, OnFinish finish, bool dontSave) {
return new AddGroup(ctx, db, name, iconid, parent, finish, dontSave);
}
private AddGroup(Context ctx, Database db, String name, int iconid, PwGroup parent, OnFinish finish, bool dontSave): base(finish) {
mCtx = ctx;
mDb = db;
mName = name;
mIconID = iconid;
mParent = parent;
mDontSave = dontSave;
_ctx = ctx;
Db = db;
_name = name;
_iconId = iconid;
Parent = parent;
DontSave = dontSave;
mFinish = new AfterAdd(this, mFinish);
OnFinishToRun = new AfterAdd(this, OnFinishToRun);
}
public override void run() {
PwDatabase pm = mDb.pm;
public override void Run() {
// Generate new group
mGroup = new PwGroup(true, true, mName, (PwIcon)mIconID);
mParent.AddGroup(mGroup, true);
Group = new PwGroup(true, true, _name, (PwIcon)_iconId);
Parent.AddGroup(Group, true);
// Commit to disk
SaveDB save = new SaveDB(mCtx, mDb, mFinish, mDontSave);
save.run();
SaveDb save = new SaveDb(_ctx, Db, OnFinishToRun, DontSave);
save.Run();
}
private class AfterAdd : OnFinish {
AddGroup addGroup;
readonly AddGroup _addGroup;
public AfterAdd(AddGroup addGroup,OnFinish finish): base(finish) {
this.addGroup = addGroup;
_addGroup = addGroup;
}
public override void run() {
public override void Run() {
if ( mSuccess ) {
if ( Success ) {
// Mark parent group dirty
addGroup.mDb.dirty.Add(addGroup.mParent);
_addGroup.Db.Dirty.Add(_addGroup.Parent);
// Add group to global list
addGroup.mDb.groups[addGroup.mGroup.Uuid] = addGroup.mGroup;
_addGroup.Db.Groups[_addGroup.Group.Uuid] = _addGroup.Group;
} else {
addGroup.mParent.Groups.Remove(addGroup.mGroup);
_addGroup.Parent.Groups.Remove(_addGroup.Group);
}
base.run();
base.Run();
}
}

View File

@@ -15,69 +15,59 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using KeePassLib.Serialization;
using KeePassLib.Keys;
namespace keepass2android
{
public class CreateDB : RunnableOnFinish {
public class CreateDb : RunnableOnFinish {
private const int DEFAULT_ENCRYPTION_ROUNDS = 1000;
private const int DefaultEncryptionRounds = 1000;
private IOConnectionInfo mIoc;
private bool mDontSave;
private Context mCtx;
private IKp2aApp mApp;
private readonly IOConnectionInfo _ioc;
private readonly bool _dontSave;
private readonly Context _ctx;
private readonly IKp2aApp _app;
public CreateDB(IKp2aApp app, Context ctx, IOConnectionInfo ioc, OnFinish finish, bool dontSave): base(finish) {
mCtx = ctx;
mIoc = ioc;
mDontSave = dontSave;
mApp = app;
public CreateDb(IKp2aApp app, Context ctx, IOConnectionInfo ioc, OnFinish finish, bool dontSave): base(finish) {
_ctx = ctx;
_ioc = ioc;
_dontSave = dontSave;
_app = app;
}
public override void run() {
Database db = mApp.CreateNewDatabase();
public override void Run() {
Database db = _app.CreateNewDatabase();
db.pm = new KeePassLib.PwDatabase();
db.KpDatabase = new KeePassLib.PwDatabase();
//Key will be changed/created immediately after creation:
CompositeKey tempKey = new CompositeKey();
db.pm.New(mIoc, tempKey);
db.KpDatabase.New(_ioc, tempKey);
db.pm.KeyEncryptionRounds = DEFAULT_ENCRYPTION_ROUNDS;
db.pm.Name = "Keepass2Android Password Database";
db.KpDatabase.KeyEncryptionRounds = DefaultEncryptionRounds;
db.KpDatabase.Name = "Keepass2Android Password Database";
// Set Database state
db.root = db.pm.RootGroup;
db.mIoc = mIoc;
db.Root = db.KpDatabase.RootGroup;
db.Ioc = _ioc;
db.Loaded = true;
db.searchHelper = new SearchDbHelper(mApp);
db.SearchHelper = new SearchDbHelper(_app);
// Add a couple default groups
AddGroup internet = AddGroup.getInstance(mCtx, db, "Internet", 1, db.pm.RootGroup, null, true);
internet.run();
AddGroup email = AddGroup.getInstance(mCtx, db, "eMail", 19, db.pm.RootGroup, null, true);
email.run();
AddGroup internet = AddGroup.GetInstance(_ctx, db, "Internet", 1, db.KpDatabase.RootGroup, null, true);
internet.Run();
AddGroup email = AddGroup.GetInstance(_ctx, db, "eMail", 19, db.KpDatabase.RootGroup, null, true);
email.Run();
// Commit changes
SaveDB save = new SaveDB(mCtx, db, mFinish, mDontSave);
mFinish = null;
save.run();
SaveDb save = new SaveDb(_ctx, db, OnFinishToRun, _dontSave);
OnFinishToRun = null;
save.Run();
}

View File

@@ -16,28 +16,19 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using KeePassLib;
namespace keepass2android
{
public class DeleteEntry : DeleteRunnable {
private PwEntry mEntry;
private readonly PwEntry _entry;
public DeleteEntry(Context ctx, IKp2aApp app, PwEntry entry, OnFinish finish):base(finish, app) {
mCtx = ctx;
mDb = app.GetDb();
mEntry = entry;
Ctx = ctx;
Db = app.GetDb();
_entry = entry;
}
@@ -45,7 +36,7 @@ namespace keepass2android
{
get
{
return CanRecycleGroup(mEntry.ParentGroup);
return CanRecycleGroup(_entry.ParentGroup);
}
}
@@ -57,15 +48,15 @@ namespace keepass2android
}
}
public override void run() {
public override void Run() {
PwDatabase pd = mDb.pm;
PwDatabase pd = Db.KpDatabase;
PwGroup pgRecycleBin = pd.RootGroup.FindGroup(pd.RecycleBinUuid, true);
bool bUpdateGroupList = false;
DateTime dtNow = DateTime.Now;
PwEntry pe = mEntry;
PwEntry pe = _entry;
PwGroup pgParent = pe.ParentGroup;
if(pgParent != null)
{
@@ -77,19 +68,19 @@ namespace keepass2android
PwDeletedObject pdo = new PwDeletedObject(pe.Uuid, dtNow);
pd.DeletedObjects.Add(pdo);
mFinish = new ActionOnFinish( (success, message) =>
{
if ( success ) {
// Mark parent dirty
if ( pgParent != null ) {
mDb.dirty.Add(pgParent);
OnFinishToRun = new ActionOnFinish((success, message) =>
{
if (success)
{
// Mark parent dirty
Db.Dirty.Add(pgParent);
}
} else {
// Let's not bother recovering from a failure to save a deleted entry. It is too much work.
mApp.SetShutdown();
}
}, this.mFinish);
else
{
// Let's not bother recovering from a failure to save a deleted entry. It is too much work.
App.SetShutdown();
}
}, OnFinishToRun);
}
else // Recycle
{
@@ -98,27 +89,25 @@ namespace keepass2android
pgRecycleBin.AddEntry(pe, true, true);
pe.Touch(false);
mFinish = new ActionOnFinish( (success, message) =>
OnFinishToRun = new ActionOnFinish( (success, message) =>
{
if ( success ) {
// Mark previous parent dirty
if ( pgParent != null ) {
mDb.dirty.Add(pgParent);
}
Db.Dirty.Add(pgParent);
// Mark new parent dirty
mDb.dirty.Add(pgRecycleBin);
Db.Dirty.Add(pgRecycleBin);
} else {
// Let's not bother recovering from a failure to save a deleted entry. It is too much work.
mApp.SetShutdown();
App.SetShutdown();
}
}, this.mFinish);
}, OnFinishToRun);
}
}
// Commit database
SaveDB save = new SaveDB(mCtx, mDb, mFinish, false);
save.run();
SaveDb save = new SaveDb(Ctx, Db, OnFinishToRun, false);
save.Run();
}

View File

@@ -16,16 +16,7 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using KeePassLib;
namespace keepass2android
@@ -33,33 +24,31 @@ namespace keepass2android
public class DeleteGroup : DeleteRunnable {
private PwGroup mGroup;
private Activity mAct;
protected bool mDontSave;
private PwGroup _group;
protected bool DontSave;
public DeleteGroup(Context ctx, IKp2aApp app, PwGroup group, Activity act, OnFinish finish)
public DeleteGroup(Context ctx, IKp2aApp app, PwGroup group, OnFinish finish)
: base(finish, app)
{
setMembers(ctx, app, group, act, false);
SetMembers(ctx, app, group, false);
}
/*
public DeleteGroup(Context ctx, Database db, PwGroup group, Activity act, OnFinish finish, bool dontSave)
: base(finish)
{
setMembers(ctx, db, group, act, dontSave);
SetMembers(ctx, db, group, act, dontSave);
}
public DeleteGroup(Context ctx, Database db, PwGroup group, OnFinish finish, bool dontSave):base(finish) {
setMembers(ctx, db, group, null, dontSave);
SetMembers(ctx, db, group, null, dontSave);
}
*/
private void setMembers(Context ctx, IKp2aApp app, PwGroup group, Activity act, bool dontSave)
private void SetMembers(Context ctx, IKp2aApp app, PwGroup group, bool dontSave)
{
base.setMembers(ctx, app.GetDb());
base.SetMembers(ctx, app.GetDb());
mGroup = group;
mAct = act;
mDontSave = dontSave;
_group = group;
DontSave = dontSave;
}
@@ -67,7 +56,7 @@ namespace keepass2android
{
get
{
return CanRecycleGroup(mGroup);
return CanRecycleGroup(_group);
}
}
@@ -80,13 +69,13 @@ namespace keepass2android
}
public override void run() {
public override void Run() {
//from KP Desktop
PwGroup pg = mGroup;
PwGroup pg = _group;
PwGroup pgParent = pg.ParentGroup;
if(pgParent == null) return; // Can't remove virtual or root group
PwDatabase pd = mDb.pm;
PwDatabase pd = Db.KpDatabase;
PwGroup pgRecycleBin = pd.RootGroup.FindGroup(pd.RecycleBinUuid, true);
pgParent.Groups.Remove(pg);
@@ -97,7 +86,7 @@ namespace keepass2android
PwDeletedObject pdo = new PwDeletedObject(pg.Uuid, DateTime.Now);
pd.DeletedObjects.Add(pdo);
mFinish = new AfterDeletePermanently(mFinish, mApp, mGroup);
OnFinishToRun = new AfterDeletePermanently(OnFinishToRun, App, _group);
}
else // Recycle
{
@@ -106,59 +95,59 @@ namespace keepass2android
pgRecycleBin.AddGroup(pg, true, true);
pg.Touch(false);
mFinish = new ActionOnFinish((success, message) =>
OnFinishToRun = new ActionOnFinish((success, message) =>
{
if ( success ) {
// Mark new parent (Recycle bin) dirty
PwGroup parent = mGroup.ParentGroup;
PwGroup parent = _group.ParentGroup;
if ( parent != null ) {
mDb.dirty.Add(parent);
Db.Dirty.Add(parent);
}
//Mark old parent dirty:
mDb.dirty.Add(pgParent);
Db.Dirty.Add(pgParent);
} else {
// Let's not bother recovering from a failure to save a deleted group. It is too much work.
mApp.SetShutdown();
App.SetShutdown();
}
}, this.mFinish);
}, OnFinishToRun);
}
// Save
SaveDB save = new SaveDB(mCtx, mDb, mFinish, mDontSave);
save.run();
SaveDb save = new SaveDb(Ctx, Db, OnFinishToRun, DontSave);
save.Run();
}
private class AfterDeletePermanently : OnFinish {
IKp2aApp mApp;
readonly IKp2aApp _app;
PwGroup mGroup;
readonly PwGroup _group;
public AfterDeletePermanently(OnFinish finish, IKp2aApp app, PwGroup group):base(finish) {
this.mApp = app;
this.mGroup = group;
_app = app;
_group = group;
}
public override void run() {
if ( mSuccess ) {
public override void Run() {
if ( Success ) {
// Remove from group global
mApp.GetDb().groups.Remove(mGroup.Uuid);
_app.GetDb().Groups.Remove(_group.Uuid);
// Remove group from the dirty global (if it is present), not a big deal if this fails (doesn't throw)
mApp.GetDb().dirty.Remove(mGroup);
_app.GetDb().Dirty.Remove(_group);
// Mark parent dirty
PwGroup parent = mGroup.ParentGroup;
PwGroup parent = _group.ParentGroup;
if ( parent != null ) {
mApp.GetDb().dirty.Add(parent);
_app.GetDb().Dirty.Add(parent);
}
} else {
// Let's not bother recovering from a failure to save a deleted group. It is too much work.
mApp.SetShutdown();
_app.SetShutdown();
}
base.run();
base.Run();
}

View File

@@ -1,50 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using KeePassLib;
namespace keepass2android
{
public abstract class DeleteRunnable : RunnableOnFinish
{
public DeleteRunnable(OnFinish finish, IKp2aApp app):base(finish)
protected DeleteRunnable(OnFinish finish, IKp2aApp app):base(finish)
{
mApp = app;
App = app;
}
protected IKp2aApp mApp;
protected IKp2aApp App;
protected Database mDb;
protected Database Db;
protected Context mCtx;
protected Context Ctx;
protected void setMembers(Context ctx, Database db)
protected void SetMembers(Context ctx, Database db)
{
mCtx = ctx;
mDb = db;
Ctx = ctx;
Db = db;
}
private bool mDeletePermanently = true;
private bool _deletePermanently = true;
public bool DeletePermanently
{
get
{
return mDeletePermanently;
return _deletePermanently;
}
set
{
mDeletePermanently = value;
_deletePermanently = value;
}
}
@@ -56,7 +45,7 @@ namespace keepass2android
protected bool CanRecycleGroup(PwGroup pgParent)
{
bool bShiftPressed = false;
PwDatabase pd = mDb.pm;
PwDatabase pd = Db.KpDatabase;
PwGroup pgRecycleBin = pd.RootGroup.FindGroup(pd.RecycleBinUuid, true);
bool bPermanent = false;
if (pgParent != null)
@@ -80,27 +69,30 @@ namespace keepass2android
protected void EnsureRecycleBin(ref PwGroup pgRecycleBin,
ref bool bGroupListUpdateRequired)
{
if ((mDb == null) || (mDb.pm == null)) { return; }
if ((Db == null) || (Db.KpDatabase == null)) { return; }
if(pgRecycleBin == mDb.pm.RootGroup)
if(pgRecycleBin == Db.KpDatabase.RootGroup)
{
pgRecycleBin = null;
}
if(pgRecycleBin == null)
{
pgRecycleBin = new PwGroup(true, true, mApp.GetResourceString(UiStringKey.RecycleBin),
PwIcon.TrashBin);
pgRecycleBin.EnableAutoType = false;
pgRecycleBin.EnableSearching = false;
pgRecycleBin.IsExpanded = false;
mDb.pm.RootGroup.AddGroup(pgRecycleBin, true);
pgRecycleBin = new PwGroup(true, true, App.GetResourceString(UiStringKey.RecycleBin),
PwIcon.TrashBin)
{
EnableAutoType = false,
EnableSearching = false,
IsExpanded = false
};
Db.KpDatabase.RootGroup.AddGroup(pgRecycleBin, true);
mDb.pm.RecycleBinUuid = pgRecycleBin.Uuid;
Db.KpDatabase.RecycleBinUuid = pgRecycleBin.Uuid;
bGroupListUpdateRequired = true;
}
else { System.Diagnostics.Debug.Assert(pgRecycleBin.Uuid.EqualsValue(mDb.pm.RecycleBinUuid)); }
else { System.Diagnostics.Debug.Assert(pgRecycleBin.Uuid.EqualsValue(Db.KpDatabase.RecycleBinUuid)); }
}
protected abstract UiStringKey QuestionsResourceId
@@ -108,31 +100,31 @@ namespace keepass2android
get;
}
public void start()
public void Start()
{
if (CanRecycle)
{
mApp.AskYesNoCancel(UiStringKey.AskDeletePermanently_title,
App.AskYesNoCancel(UiStringKey.AskDeletePermanently_title,
QuestionsResourceId,
new EventHandler<DialogClickEventArgs>((dlgSender, dlgEvt) =>
{
DeletePermanently = true;
ProgressTask pt = new ProgressTask(mApp, mCtx, this, UiStringKey.saving_database);
pt.run();
}),
new EventHandler<DialogClickEventArgs>((dlgSender, dlgEvt) => {
DeletePermanently = false;
ProgressTask pt = new ProgressTask(mApp, mCtx, this, UiStringKey.saving_database);
pt.run();
}),
new EventHandler<DialogClickEventArgs>((dlgSender, dlgEvt) => {}),
mCtx);
(dlgSender, dlgEvt) =>
{
DeletePermanently = true;
ProgressTask pt = new ProgressTask(App, Ctx, this, UiStringKey.saving_database);
pt.run();
},
(dlgSender, dlgEvt) => {
DeletePermanently = false;
ProgressTask pt = new ProgressTask(App, Ctx, this, UiStringKey.saving_database);
pt.run();
},
(dlgSender, dlgEvt) => {},
Ctx);
} else
{
ProgressTask pt = new ProgressTask(mApp, mCtx, this, UiStringKey.saving_database);
ProgressTask pt = new ProgressTask(App, Ctx, this, UiStringKey.saving_database);
pt.run();
}
}

View File

@@ -16,34 +16,21 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace keepass2android
{
public abstract class FileOnFinish : OnFinish {
private String mFilename = "";
public FileOnFinish(FileOnFinish finish):base(finish) {
private String _filename = "";
protected FileOnFinish(FileOnFinish finish):base(finish) {
}
public void setFilename(String filename) {
mFilename = filename;
public string Filename
{
get { return _filename; }
set { _filename = value; }
}
public String getFilename() {
return mFilename;
}
}
}

View File

@@ -16,98 +16,85 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Preferences;
using KeePassLib.Serialization;
namespace keepass2android
{
public class LoadDB : RunnableOnFinish {
private IOConnectionInfo mIoc;
private String mPass;
private String mKey;
private IKp2aApp mApp;
private Context mCtx;
private bool mRememberKeyfile;
public class LoadDb : RunnableOnFinish {
private readonly IOConnectionInfo _ioc;
private readonly String _pass;
private readonly String _key;
private readonly IKp2aApp _app;
private readonly bool _rememberKeyfile;
public LoadDB(IKp2aApp app, Context ctx, IOConnectionInfo ioc, String pass, String key, OnFinish finish): base(finish)
public LoadDb(IKp2aApp app, IOConnectionInfo ioc, String pass, String key, OnFinish finish): base(finish)
{
mApp = app;
mCtx = ctx;
mIoc = ioc;
mPass = pass;
mKey = key;
_app = app;
_ioc = ioc;
_pass = pass;
_key = key;
mRememberKeyfile = app.GetBooleanPreference(PreferenceKey.remember_keyfile);
_rememberKeyfile = app.GetBooleanPreference(PreferenceKey.remember_keyfile);
}
public override void run ()
public override void Run ()
{
try {
mApp.GetDb().LoadData (mApp, mIoc, mPass, mKey, mStatus);
_app.GetDb().LoadData (_app, _ioc, _pass, _key, Status);
saveFileData (mIoc, mKey);
SaveFileData (_ioc, _key);
} catch (KeyFileException) {
finish(false, /*TODO Localize: use Keepass error text KPRes.KeyFileError (including "or invalid format")*/ mApp.GetResourceString(UiStringKey.keyfile_does_not_exist));
Finish(false, /*TODO Localize: use Keepass error text KPRes.KeyFileError (including "or invalid format")*/ _app.GetResourceString(UiStringKey.keyfile_does_not_exist));
}
catch (Exception e) {
finish(false, "An error occured: " + e.Message);
Finish(false, "An error occured: " + e.Message);
return;
}
/* catch (InvalidPasswordException e) {
finish(false, mCtx.GetString(Resource.String.InvalidPassword));
finish(false, Ctx.GetString(Resource.String.InvalidPassword));
return;
} catch (FileNotFoundException e) {
finish(false, mCtx.GetString(Resource.String.FileNotFound));
finish(false, Ctx.GetString(Resource.String.FileNotFound));
return;
} catch (IOException e) {
finish(false, e.getMessage());
return;
} catch (KeyFileEmptyException e) {
finish(false, mCtx.GetString(Resource.String.keyfile_is_empty));
finish(false, Ctx.GetString(Resource.String.keyfile_is_empty));
return;
} catch (InvalidAlgorithmException e) {
finish(false, mCtx.GetString(Resource.String.invalid_algorithm));
finish(false, Ctx.GetString(Resource.String.invalid_algorithm));
return;
} catch (InvalidKeyFileException e) {
finish(false, mCtx.GetString(Resource.String.keyfile_does_not_exist));
finish(false, Ctx.GetString(Resource.String.keyfile_does_not_exist));
return;
} catch (InvalidDBSignatureException e) {
finish(false, mCtx.GetString(Resource.String.invalid_db_sig));
finish(false, Ctx.GetString(Resource.String.invalid_db_sig));
return;
} catch (InvalidDBVersionException e) {
finish(false, mCtx.GetString(Resource.String.unsupported_db_version));
finish(false, Ctx.GetString(Resource.String.unsupported_db_version));
return;
} catch (InvalidDBException e) {
finish(false, mCtx.GetString(Resource.String.error_invalid_db));
finish(false, Ctx.GetString(Resource.String.error_invalid_db));
return;
} catch (OutOfMemoryError e) {
finish(false, mCtx.GetString(Resource.String.error_out_of_memory));
finish(false, Ctx.GetString(Resource.String.error_out_of_memory));
return;
}
*/
finish(true);
Finish(true);
}
private void saveFileData(IOConnectionInfo ioc, String key) {
private void SaveFileData(IOConnectionInfo ioc, String key) {
if (!mRememberKeyfile)
if (!_rememberKeyfile)
{
key = "";
}
mApp.StoreOpenedFileAsRecent(ioc, key);
_app.StoreOpenedFileAsRecent(ioc, key);
}

View File

@@ -16,72 +16,65 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace keepass2android
{
public abstract class OnFinish
{
protected bool mSuccess;
protected String mMessage;
protected bool Success;
protected String Message;
protected OnFinish mOnFinish;
protected Handler mHandler;
public OnFinish() {
protected OnFinish BaseOnFinish;
protected Handler Handler;
protected OnFinish() {
}
protected OnFinish(Handler handler) {
BaseOnFinish = null;
Handler = handler;
}
protected OnFinish(OnFinish finish, Handler handler) {
BaseOnFinish = finish;
Handler = handler;
}
protected OnFinish(OnFinish finish) {
BaseOnFinish = finish;
Handler = null;
}
public OnFinish(Handler handler) {
mOnFinish = null;
mHandler = handler;
public void SetResult(bool success, String message) {
Success = success;
Message = message;
}
public OnFinish(OnFinish finish, Handler handler) {
mOnFinish = finish;
mHandler = handler;
public void SetResult(bool success) {
Success = success;
}
public OnFinish(OnFinish finish) {
mOnFinish = finish;
mHandler = null;
}
public void setResult(bool success, String message) {
mSuccess = success;
mMessage = message;
}
public void setResult(bool success) {
mSuccess = success;
}
public virtual void run() {
if ( mOnFinish != null ) {
public virtual void Run() {
if ( BaseOnFinish != null ) {
// Pass on result on call finish
mOnFinish.setResult(mSuccess, mMessage);
BaseOnFinish.SetResult(Success, Message);
if ( mHandler != null ) {
mHandler.Post(mOnFinish.run);
if ( Handler != null ) {
Handler.Post(BaseOnFinish.Run);
} else {
mOnFinish.run();
BaseOnFinish.Run();
}
}
}
protected void displayMessage(Context ctx) {
displayMessage(ctx, mMessage);
protected void DisplayMessage(Context ctx) {
DisplayMessage(ctx, Message);
}
public static void displayMessage(Context ctx, string message)
public static void DisplayMessage(Context ctx, string message)
{
if ( !String.IsNullOrEmpty(message) ) {
Toast.MakeText(ctx, message, ToastLength.Long).Show();

View File

@@ -15,48 +15,38 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace keepass2android
{
public abstract class RunnableOnFinish {
public OnFinish mFinish;
public UpdateStatus mStatus;
public RunnableOnFinish(OnFinish finish) {
mFinish = finish;
public OnFinish OnFinishToRun;
public UpdateStatus Status;
protected RunnableOnFinish(OnFinish finish) {
OnFinishToRun = finish;
}
protected void finish(bool result, String message) {
if ( mFinish != null ) {
mFinish.setResult(result, message);
mFinish.run();
protected void Finish(bool result, String message) {
if ( OnFinishToRun != null ) {
OnFinishToRun.SetResult(result, message);
OnFinishToRun.Run();
}
}
protected void finish(bool result) {
if ( mFinish != null ) {
mFinish.setResult(result);
mFinish.run();
protected void Finish(bool result) {
if ( OnFinishToRun != null ) {
OnFinishToRun.SetResult(result);
OnFinishToRun.Run();
}
}
public void setStatus(UpdateStatus status) {
mStatus = status;
public void SetStatus(UpdateStatus status) {
Status = status;
}
abstract public void run();
abstract public void Run();
}
}

View File

@@ -15,46 +15,37 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace keepass2android
{
public class SaveDB : RunnableOnFinish {
private Database mDb;
private bool mDontSave;
private Context mCtx;
public class SaveDb : RunnableOnFinish {
private readonly Database _db;
private readonly bool _dontSave;
private readonly Context _ctx;
public SaveDB(Context ctx, Database db, OnFinish finish, bool dontSave): base(finish) {
mCtx = ctx;
mDb = db;
mDontSave = dontSave;
public SaveDb(Context ctx, Database db, OnFinish finish, bool dontSave): base(finish) {
_ctx = ctx;
_db = db;
_dontSave = dontSave;
}
public SaveDB(Context ctx, Database db, OnFinish finish):base(finish) {
mCtx = ctx;
mDb = db;
mDontSave = false;
public SaveDb(Context ctx, Database db, OnFinish finish):base(finish) {
_ctx = ctx;
_db = db;
_dontSave = false;
}
public override void run ()
public override void Run ()
{
if (! mDontSave) {
if (! _dontSave) {
try {
mDb.SaveData (mCtx);
if (mDb.mIoc.IsLocalFile())
mDb.mLastChangeDate = System.IO.File.GetLastWriteTimeUtc(mDb.mIoc.Path);
_db.SaveData (_ctx);
if (_db.Ioc.IsLocalFile())
_db.LastChangeDate = System.IO.File.GetLastWriteTimeUtc(_db.Ioc.Path);
} catch (Exception e) {
/* TODO KPDesktop:
* catch(Exception exSave)
@@ -63,12 +54,12 @@ namespace keepass2android
bSuccess = false;
}
*/
finish (false, e.Message);
Finish (false, e.Message);
return;
}
}
finish(true);
Finish(true);
}
}

View File

@@ -15,16 +15,7 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using KeePassLib;
using KeePassLib.Keys;
@@ -32,40 +23,40 @@ namespace keepass2android
{
public class SetPassword : RunnableOnFinish {
private String mPassword;
private String mKeyfile;
private Database mDb;
private bool mDontSave;
private Context mCtx;
private readonly String _password;
private readonly String _keyfile;
private readonly Database _db;
private readonly bool _dontSave;
private readonly Context _ctx;
public SetPassword(Context ctx, Database db, String password, String keyfile, OnFinish finish): base(finish) {
mCtx = ctx;
mDb = db;
mPassword = password;
mKeyfile = keyfile;
mDontSave = false;
_ctx = ctx;
_db = db;
_password = password;
_keyfile = keyfile;
_dontSave = false;
}
public SetPassword(Context ctx, Database db, String password, String keyfile, OnFinish finish, bool dontSave): base(finish) {
mCtx = ctx;
mDb = db;
mPassword = password;
mKeyfile = keyfile;
mDontSave = dontSave;
_ctx = ctx;
_db = db;
_password = password;
_keyfile = keyfile;
_dontSave = dontSave;
}
public override void run ()
public override void Run ()
{
PwDatabase pm = mDb.pm;
PwDatabase pm = _db.KpDatabase;
CompositeKey newKey = new CompositeKey ();
if (String.IsNullOrEmpty (mPassword) == false) {
newKey.AddUserKey (new KcpPassword (mPassword));
if (String.IsNullOrEmpty (_password) == false) {
newKey.AddUserKey (new KcpPassword (_password));
}
if (String.IsNullOrEmpty (mKeyfile) == false) {
if (String.IsNullOrEmpty (_keyfile) == false) {
try {
newKey.AddUserKey (new KcpKeyFile (mKeyfile));
} catch (Exception exKF) {
newKey.AddUserKey (new KcpKeyFile (_keyfile));
} catch (Exception) {
//TODO MessageService.ShowWarning (strKeyFile, KPRes.KeyFileError, exKF);
return;
}
@@ -78,29 +69,29 @@ namespace keepass2android
pm.MasterKey = newKey;
// Save Database
mFinish = new AfterSave(previousKey, previousMasterKeyChanged, pm, mFinish);
SaveDB save = new SaveDB(mCtx, mDb, mFinish, mDontSave);
save.run();
OnFinishToRun = new AfterSave(previousKey, previousMasterKeyChanged, pm, OnFinishToRun);
SaveDb save = new SaveDb(_ctx, _db, OnFinishToRun, _dontSave);
save.Run();
}
private class AfterSave : OnFinish {
private CompositeKey mBackup;
private DateTime mPreviousKeyChanged;
private PwDatabase mDb;
private readonly CompositeKey _backup;
private readonly DateTime _previousKeyChanged;
private PwDatabase _db;
public AfterSave(CompositeKey backup, DateTime previousKeyChanged, PwDatabase db, OnFinish finish): base(finish) {
mPreviousKeyChanged = previousKeyChanged;
mBackup = backup;
mDb = db;
_previousKeyChanged = previousKeyChanged;
_backup = backup;
_db = db;
}
public override void run() {
if ( ! mSuccess ) {
mDb.MasterKey = mBackup;
mDb.MasterKeyChanged = mPreviousKeyChanged;
public override void Run() {
if ( ! Success ) {
_db.MasterKey = _backup;
_db.MasterKeyChanged = _previousKeyChanged;
}
base.run();
base.Run();
}
}

View File

@@ -15,81 +15,67 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using KeePassLib;
namespace keepass2android
{
public class UpdateEntry : RunnableOnFinish {
private Database mDb;
private PwEntry mOldE;
private PwEntry mNewE;
private Context mCtx;
private readonly Database _db;
private readonly Context _ctx;
public UpdateEntry(Context ctx, Database db, PwEntry oldE, PwEntry newE, OnFinish finish):base(finish) {
mCtx = ctx;
mDb = db;
mOldE = oldE;
mNewE = newE;
_ctx = ctx;
_db = db;
mFinish = new AfterUpdate(oldE, newE, db, finish);
OnFinishToRun = new AfterUpdate(oldE, newE, db, finish);
}
public override void run() {
public override void Run() {
// Commit to disk
SaveDB save = new SaveDB(mCtx, mDb, mFinish);
save.run();
SaveDb save = new SaveDb(_ctx, _db, OnFinishToRun);
save.Run();
}
private class AfterUpdate : OnFinish {
private PwEntry mBackup;
private PwEntry mUpdatedEntry;
private Database mDb;
private readonly PwEntry _backup;
private readonly PwEntry _updatedEntry;
private readonly Database _db;
public AfterUpdate(PwEntry backup, PwEntry updatedEntry, Database db, OnFinish finish):base(finish) {
mBackup = backup;
mUpdatedEntry = updatedEntry;
mDb = db;
_backup = backup;
_updatedEntry = updatedEntry;
_db = db;
}
public override void run() {
if ( mSuccess ) {
public override void Run() {
if ( Success ) {
// Mark group dirty if title, icon or Expiry stuff changes
if ( ! mBackup.Strings.ReadSafe (PwDefs.TitleField).Equals(mUpdatedEntry.Strings.ReadSafe (PwDefs.TitleField))
|| ! mBackup.IconId.Equals(mUpdatedEntry.IconId)
|| ! mBackup.CustomIconUuid.EqualsValue(mUpdatedEntry.CustomIconUuid)
|| mBackup.Expires != mUpdatedEntry.Expires
|| (mBackup.Expires && (! mBackup.ExpiryTime.Equals(mUpdatedEntry.ExpiryTime)))
if ( ! _backup.Strings.ReadSafe (PwDefs.TitleField).Equals(_updatedEntry.Strings.ReadSafe (PwDefs.TitleField))
|| ! _backup.IconId.Equals(_updatedEntry.IconId)
|| ! _backup.CustomIconUuid.EqualsValue(_updatedEntry.CustomIconUuid)
|| _backup.Expires != _updatedEntry.Expires
|| (_backup.Expires && (! _backup.ExpiryTime.Equals(_updatedEntry.ExpiryTime)))
)
{
PwGroup parent = mUpdatedEntry.ParentGroup;
PwGroup parent = _updatedEntry.ParentGroup;
if ( parent != null ) {
// Mark parent group dirty
mDb.dirty.Add(parent);
_db.Dirty.Add(parent);
}
}
} else {
// If we fail to save, back out changes to global structure
//TODO test fail
mUpdatedEntry.AssignProperties(mBackup, false, true, false);
_updatedEntry.AssignProperties(_backup, false, true, false);
}
base.run();
base.Run();
}
}