Added Check if Database changed

This commit is contained in:
PhilippC 2013-03-15 07:07:45 +01:00
parent 44ec0e0a04
commit b4784b2128
9 changed files with 115 additions and 15 deletions

View File

@ -43,12 +43,14 @@ namespace keepass2android
public PwGroup root;
public PwDatabase pm;
public IOConnectionInfo mIoc;
public DateTime mLastChangeDate;
public SearchDbHelper searchHelper;
public DrawableFactory drawFactory = new DrawableFactory();
private bool loaded = false;
private bool mReloadRequested = false;
public bool Loaded {
get { return loaded;}
@ -73,20 +75,64 @@ namespace keepass2android
}
}
public bool DidOpenFileChange()
{
if ((Loaded == false) || (mIoc.IsLocalFile() == false))
{
return false;
}
return System.IO.File.GetLastWriteTimeUtc(mIoc.Path) > mLastChangeDate;
}
public void CheckForOpenFileChanged(Activity activity)
{
if (DidOpenFileChange())
{
if (mReloadRequested)
{
activity.SetResult(KeePass.EXIT_RELOAD_DB);
activity.Finish();
}
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.SetTitle(activity.GetString(Resource.String.AskReloadFile_title));
builder.SetMessage(activity.GetString(Resource.String.AskReloadFile));
builder.SetPositiveButton(activity.GetString(Android.Resource.String.Yes), new EventHandler<DialogClickEventArgs>((dlgSender, dlgEvt) =>
{
mReloadRequested = true;
activity.SetResult(KeePass.EXIT_RELOAD_DB);
activity.Finish();
}));
builder.SetNegativeButton(activity.GetString(Android.Resource.String.No), new EventHandler<DialogClickEventArgs>((dlgSender, dlgEvt) =>
{
}));
Dialog dialog = builder.Create();
dialog.Show();
}
}
public void LoadData (Context ctx, IOConnectionInfo iocInfo, String password, String keyfile, UpdateStatus status)
public void LoadData(Context ctx, IOConnectionInfo iocInfo, String password, String keyfile, UpdateStatus status)
{
mIoc = iocInfo;
KeePassLib.PwDatabase pwDatabase = new KeePassLib.PwDatabase ();
KeePassLib.PwDatabase pwDatabase = new KeePassLib.PwDatabase();
KeePassLib.Keys.CompositeKey key = new KeePassLib.Keys.CompositeKey ();
key.AddUserKey (new KeePassLib.Keys.KcpPassword (password));
if (!String.IsNullOrEmpty (keyfile)) {
KeePassLib.Keys.CompositeKey key = new KeePassLib.Keys.CompositeKey();
key.AddUserKey(new KeePassLib.Keys.KcpPassword(password));
if (!String.IsNullOrEmpty(keyfile))
{
try { key.AddUserKey(new KeePassLib.Keys.KcpKeyFile(keyfile)); }
catch(Exception)
try
{
key.AddUserKey(new KeePassLib.Keys.KcpKeyFile(keyfile));
} catch (Exception)
{
throw new KeyFileException();
}
@ -94,6 +140,14 @@ namespace keepass2android
pwDatabase.Open(iocInfo, key, status);
if (iocInfo.IsLocalFile())
{
mLastChangeDate = System.IO.File.GetLastWriteTimeUtc(iocInfo.Path);
} else
{
mLastChangeDate = DateTime.MinValue;
}
root = pwDatabase.RootGroup;
populateGlobals(root);
@ -210,6 +264,7 @@ namespace keepass2android
mIoc = null;
loaded = false;
locked = false;
mReloadRequested = false;
}
public void markAllGroupsAsDirty() {

View File

@ -47,6 +47,7 @@ namespace keepass2android
public const Android.App.Result EXIT_CLOSE_AFTER_SEARCH = Android.App.Result.FirstUser+6;
public const Android.App.Result EXIT_CHANGE_DB = Android.App.Result.FirstUser+7;
public const Android.App.Result EXIT_FORCE_LOCK_AND_CHANGE_DB = Android.App.Result.FirstUser+8;
public const Android.App.Result EXIT_RELOAD_DB = Android.App.Result.FirstUser+9;
protected override void OnCreate (Bundle bundle)
{

View File

@ -40,10 +40,15 @@ namespace keepass2android
mIoc = App.getDB().mIoc;
}
protected override void OnResume() {
protected override void OnResume()
{
base.OnResume();
TimeoutHelper.checkShutdown(this, mIoc);
if (TimeoutHelper.checkShutdown(this, mIoc))
return;
App.getDB().CheckForOpenFileChanged(this);
}

View File

@ -50,12 +50,17 @@ namespace keepass2android
{
}
protected override void OnResume() {
protected override void OnResume()
{
base.OnResume();
TimeoutHelper.checkShutdown(this, mIoc);
if (TimeoutHelper.checkShutdown(this, mIoc))
return;
App.getDB().CheckForOpenFileChanged(this);
}
}
}

View File

@ -239,7 +239,29 @@ namespace keepass2android
App.getDB().Locked = false;
LaunchNextActivity();
break;
case KeePass.EXIT_RELOAD_DB:
//if the activity was killed, fill password/keyfile so the user can directly hit load again
if (App.getDB().Loaded)
{
if (App.getDB().pm.MasterKey.ContainsType(typeof(KcpPassword)))
{
KcpPassword kcpPassword = (KcpPassword)App.getDB().pm.MasterKey.GetUserKey(typeof(KcpPassword));
String password = kcpPassword.Password.ReadString();
setEditText(Resource.Id.password, password);
}
if (App.getDB().pm.MasterKey.ContainsType(typeof(KcpKeyFile)))
{
KcpKeyFile kcpKeyfile = (KcpKeyFile)App.getDB().pm.MasterKey.GetUserKey(typeof(KcpKeyFile));
setEditText(Resource.Id.pass_keyfile, kcpKeyfile.Path);
}
}
unloadDatabase();
break;
case Android.App.Result.Ok:
if (requestCode == Intents.REQUEST_CODE_FILE_BROWSE) {
String filename = data.DataString;

View File

@ -1021,6 +1021,12 @@ namespace keepass2android
// aapt resource value: 0x7f0500eb
public const int AskOverwriteBinary_yes = 2131034347;
// aapt resource value: 0x7f0500f3
public const int AskReloadFile = 2131034355;
// aapt resource value: 0x7f0500f2
public const int AskReloadFile_title = 2131034354;
// aapt resource value: 0x7f0500ed
public const int AttachFailed = 2131034349;

View File

@ -212,6 +212,8 @@
<string name="AskDeletePermanentlyEntry">Do you want to delete this entry permanently? Press No to recycle.</string>
<string name="AskDeletePermanentlyGroup">Do you want to delete this group permanently? Press No to recycle.</string>
<string name="AskDeletePermanently_title">Delete permanently?</string>
<string name="AskReloadFile_title">Reload file?</string>
<string name="AskReloadFile">The file which is currently open was changed by another program. Do you want to reload it?</string>
<string-array name="clipboard_timeout_options">
<item>30 seconds</item>
<item>1 minute</item>

View File

@ -53,6 +53,8 @@ namespace keepass2android
if (! mDontSave) {
try {
mDb.SaveData (mCtx);
if (mDb.mIoc.IsLocalFile())
mDb.mLastChangeDate = System.IO.File.GetLastWriteTimeUtc(mDb.mIoc.Path);
} catch (Exception e) {
/* TODO KPDesktop:
* catch(Exception exSave)

View File

@ -91,13 +91,15 @@ namespace keepass2android
return ioc.GetDisplayName() != other.GetDisplayName();
}
public static void checkShutdown(Activity act, IOConnectionInfo ioc) {
public static bool checkShutdown(Activity act, IOConnectionInfo ioc) {
if (( App.getDB().Loaded && (App.isShutdown() || App.getDB().Locked) )
|| (iocChanged(ioc, App.getDB().mIoc))) //file was changed from ActionSend-Intent
{
act.SetResult(KeePass.EXIT_LOCK);
act.Finish();
return true;
}
return false;
}
}
}