Made pre-loading optional

Refactoring of PasswordActivity/QuickUnlock lifecycle
This commit is contained in:
AlexVallat 2013-08-07 18:34:43 +01:00
parent 518479904a
commit b9aad79b62
9 changed files with 595 additions and 586 deletions

View File

@ -109,7 +109,7 @@ namespace keepass2android
var filename = fileStorage.GetFilenameWithoutPathAndExt(iocInfo); var filename = fileStorage.GetFilenameWithoutPathAndExt(iocInfo);
try try
{ {
pwDatabase.Open(databaseData, filename, iocInfo, compositeKey, status); pwDatabase.Open(databaseData ?? fileStorage.OpenFileForRead(iocInfo), filename, iocInfo, compositeKey, status);
} }
catch (Exception) catch (Exception)
{ {
@ -118,8 +118,11 @@ namespace keepass2android
//if we don't get a password, we don't know whether this means "empty password" or "no password" //if we don't get a password, we don't know whether this means "empty password" or "no password"
//retry without password: //retry without password:
compositeKey.RemoveUserKey(compositeKey.GetUserKey(typeof (KcpPassword))); compositeKey.RemoveUserKey(compositeKey.GetUserKey(typeof (KcpPassword)));
databaseData.Seek(0, SeekOrigin.Begin); if (databaseData != null)
pwDatabase.Open(databaseData, filename, iocInfo, compositeKey, status); {
databaseData.Seek(0, SeekOrigin.Begin);
}
pwDatabase.Open(databaseData ?? fileStorage.OpenFileForRead(iocInfo), filename, iocInfo, compositeKey, status);
} }
else throw; else throw;
} }

View File

@ -48,7 +48,7 @@ namespace keepass2android
try try
{ {
StatusLogger.UpdateMessage(UiStringKey.loading_database); StatusLogger.UpdateMessage(UiStringKey.loading_database);
_app.LoadDatabase(_ioc, _databaseData.Result, _pass, _key, StatusLogger); _app.LoadDatabase(_ioc, _databaseData == null ? null : _databaseData.Result, _pass, _key, StatusLogger);
SaveFileData (_ioc, _key); SaveFileData (_ioc, _key);
} catch (KeyFileException) { } catch (KeyFileException) {

View File

@ -37,12 +37,9 @@ namespace keepass2android
public const Result ExitLock = Result.FirstUser+1; public const Result ExitLock = Result.FirstUser+1;
public const Result ExitRefresh = Result.FirstUser+2; public const Result ExitRefresh = Result.FirstUser+2;
public const Result ExitRefreshTitle = Result.FirstUser+3; public const Result ExitRefreshTitle = Result.FirstUser+3;
public const Result ExitForceLock = Result.FirstUser+4; public const Result ExitCloseAfterTaskComplete = Result.FirstUser+4;
public const Result ExitQuickUnlock = Result.FirstUser+5; public const Result ExitChangeDb = Result.FirstUser+5; // NOTE: Nothing is currently using this, but in the future a "Change Database" menu option might.
public const Result ExitCloseAfterTaskComplete = Result.FirstUser+6; public const Result ExitReloadDb = Result.FirstUser+6;
public const Result ExitChangeDb = Result.FirstUser+7;
public const Result ExitForceLockAndChangeDb = Result.FirstUser+8;
public const Result ExitReloadDb = Result.FirstUser+9;
AppTask _appTask; AppTask _appTask;

View File

@ -59,6 +59,8 @@ namespace keepass2android
private bool _rememberKeyfile; private bool _rememberKeyfile;
ISharedPreferences _prefs; ISharedPreferences _prefs;
private bool _started;
public PasswordActivity (IntPtr javaReference, JniHandleOwnership transfer) public PasswordActivity (IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer) : base(javaReference, transfer)
{ {
@ -138,27 +140,22 @@ namespace keepass2android
//NOTE: original code from k eepassdroid used switch ((Android.App.Result)requestCode) { (but doesn't work here, although k eepassdroid works) //NOTE: original code from k eepassdroid used switch ((Android.App.Result)requestCode) { (but doesn't work here, although k eepassdroid works)
switch(resultCode) { switch(resultCode) {
case KeePass.ExitNormal: // Returned to this screen using the Back key, treat as locking the database
App.Kp2a.LockDatabase();
break;
case KeePass.ExitLock: case KeePass.ExitLock:
// The database has already been locked, and the quick unlock screen will be shown if appropriate // The database has already been locked, and the quick unlock screen will be shown if appropriate
break; break;
case KeePass.ExitForceLock: case KeePass.ExitChangeDb:
App.Kp2a.LockDatabase(false);
break;
case KeePass.ExitForceLockAndChangeDb:
case KeePass.ExitChangeDb: // What's the difference between this and ExitForceLockAndChangeDb?
case KeePass.ExitNormal: // Returned to this screen using the Back key, treat as exiting the database
App.Kp2a.LockDatabase(false); App.Kp2a.LockDatabase(false);
Finish(); Finish();
break; break;
case KeePass.ExitCloseAfterTaskComplete: case KeePass.ExitCloseAfterTaskComplete:
// Do not lock the database
SetResult(KeePass.ExitCloseAfterTaskComplete); SetResult(KeePass.ExitCloseAfterTaskComplete);
Finish(); Finish();
break; break;
case KeePass.ExitQuickUnlock:
App.Kp2a.UnlockDatabase();
LaunchNextActivity();
break;
case KeePass.ExitReloadDb: case KeePass.ExitReloadDb:
//if the activity was killed, fill password/keyfile so the user can directly hit load again //if the activity was killed, fill password/keyfile so the user can directly hit load again
if (App.Kp2a.GetDb().Loaded) if (App.Kp2a.GetDb().Loaded)
@ -180,8 +177,9 @@ namespace keepass2android
SetEditText(Resource.Id.pass_keyfile, kcpKeyfile.Path); SetEditText(Resource.Id.pass_keyfile, kcpKeyfile.Path);
} }
} }
App.Kp2a.LockDatabase(false);
break; break;
case Result.Ok: case Result.Ok: // Key file browse dialog OK'ed.
if (requestCode == Intents.RequestCodeFileBrowseForKeyfile) { if (requestCode == Intents.RequestCodeFileBrowseForKeyfile) {
string filename = Util.IntentToFilename(data); string filename = Util.IntentToFilename(data);
if (filename != null) { if (filename != null) {
@ -258,6 +256,13 @@ namespace keepass2android
} }
} }
if (App.Kp2a.GetDb().Loaded && App.Kp2a.GetDb().Ioc != null &&
App.Kp2a.GetDb().Ioc.GetDisplayName() != _ioConnection.GetDisplayName())
{
// A different database is currently loaded, unload it before loading the new one requested
App.Kp2a.LockDatabase(false);
}
AppTask = AppTask.GetTaskInOnCreate(savedInstanceState, Intent); AppTask = AppTask.GetTaskInOnCreate(savedInstanceState, Intent);
SetContentView(Resource.Layout.password); SetContentView(Resource.Layout.password);
@ -363,19 +368,7 @@ namespace keepass2android
protected override void OnStart() protected override void OnStart()
{ {
base.OnStart(); base.OnStart();
_started = true;
if (App.Kp2a.QuickUnlockEnabled && App.Kp2a.QuickLocked)
{
Intent i = new Intent(this, typeof(QuickUnlock));
PutIoConnectionToIntent(_ioConnection, i);
Kp2aLog.Log("Starting QuickUnlock");
StartActivityForResult(i, 0);
}
else
{
// Create task to kick off file loading while the user enters the password
_loadDbTask = Task.Factory.StartNew<MemoryStream>(LoadDbFile);
}
} }
private MemoryStream LoadDbFile() private MemoryStream LoadDbFile()
@ -410,27 +403,33 @@ namespace keepass2android
AppTask.ToBundle(outState); AppTask.ToBundle(outState);
} }
/* protected override void OnResume()
protected override void OnResume() { {
base.OnResume(); base.OnResume();
if (_startedWithActivityResult)
return;
if (App.Kp2a.GetDb().Loaded && (App.Kp2a.GetDb().Ioc != null) // OnResume is run every time the activity comes to the foreground. This code should only run when the activity is started (OnStart), but must
&& (_ioConnection != null) && (App.Kp2a.GetDb().Ioc.GetDisplayName() == _ioConnection.GetDisplayName())) // be run in OnResume rather than OnStart so that it always occurrs after OnActivityResult (when re-creating a killed activity, OnStart occurs before OnActivityResult)
if (_started)
{ {
if (App.Kp2a.QuickLocked == false) _started = false;
if (App.Kp2a.DatabaseIsUnlocked)
{ {
LaunchNextActivity(); LaunchNextActivity();
} }
else else if (App.Kp2a.QuickUnlockEnabled && App.Kp2a.QuickLocked)
{ {
TryStartQuickUnlock(); var i = new Intent(this, typeof(QuickUnlock));
PutIoConnectionToIntent(_ioConnection, i);
Kp2aLog.Log("Starting QuickUnlock");
StartActivityForResult(i, 0);
}
else if (_loadDbTask == null && _prefs.GetBoolean(GetString(Resource.String.PreloadDatabaseEnabled_key), true))
{
// Create task to kick off file loading while the user enters the password
_loadDbTask = Task.Factory.StartNew<MemoryStream>(LoadDbFile);
} }
} }
} }
* */
private void RetrieveSettings() { private void RetrieveSettings() {
String defaultFilename = _prefs.GetString(KeyDefaultFilename, ""); String defaultFilename = _prefs.GetString(KeyDefaultFilename, "");

View File

@ -72,8 +72,6 @@ namespace keepass2android
keyboard.ShowSoftInput(pwd, 0); keyboard.ShowSoftInput(pwd, 0);
}, 50); }, 50);
SetResult(KeePass.ExitChangeDb);
Button btnUnlock = (Button)FindViewById(Resource.Id.QuickUnlock_button); Button btnUnlock = (Button)FindViewById(Resource.Id.QuickUnlock_button);
btnUnlock.Click += (object sender, EventArgs e) => btnUnlock.Click += (object sender, EventArgs e) =>
{ {
@ -82,11 +80,11 @@ namespace keepass2android
String expectedPasswordPart = password.Substring(Math.Max(0,password.Length-quickUnlockLength),Math.Min(password.Length, quickUnlockLength)); String expectedPasswordPart = password.Substring(Math.Max(0,password.Length-quickUnlockLength),Math.Min(password.Length, quickUnlockLength));
if (pwd.Text == expectedPasswordPart) if (pwd.Text == expectedPasswordPart)
{ {
SetResult(KeePass.ExitQuickUnlock); App.Kp2a.UnlockDatabase();
} }
else else
{ {
SetResult(KeePass.ExitForceLock); App.Kp2a.LockDatabase(false);
Toast.MakeText(this, GetString(Resource.String.QuickUnlock_fail), ToastLength.Long).Show(); Toast.MakeText(this, GetString(Resource.String.QuickUnlock_fail), ToastLength.Long).Show();
} }
Finish(); Finish();
@ -95,22 +93,10 @@ namespace keepass2android
Button btnLock = (Button)FindViewById(Resource.Id.QuickUnlock_buttonLock); Button btnLock = (Button)FindViewById(Resource.Id.QuickUnlock_buttonLock);
btnLock.Click += (object sender, EventArgs e) => btnLock.Click += (object sender, EventArgs e) =>
{ {
SetResult(KeePass.ExitForceLockAndChangeDb); App.Kp2a.LockDatabase(false);
Finish(); Finish();
}; };
} }
protected override void OnResume()
{
base.OnResume();
if ( ! App.Kp2a.GetDb().Loaded ) {
SetResult(KeePass.ExitChangeDb);
Finish();
return;
}
}
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -92,4 +92,7 @@
<string name="ShowUnlockedNotification_key">ShowUnlockedNotification</string> <string name="ShowUnlockedNotification_key">ShowUnlockedNotification</string>
<bool name="ShowUnlockedNotification_default">true</bool> <bool name="ShowUnlockedNotification_default">true</bool>
<string name="PreloadDatabaseEnabled_key">PreloadDatabaseEnabled</string>
<bool name="PreloadDatabaseEnabled_default">true</bool>
</resources> </resources>

View File

@ -232,8 +232,11 @@
<string name="ShowUnlockedNotification_title">Notification while unlocked</string> <string name="ShowUnlockedNotification_title">Notification while unlocked</string>
<string name="ShowUnlockedNotification_summary">Show an ongoing notification while the database is unlocked.</string> <string name="ShowUnlockedNotification_summary">Show an ongoing notification while the database is unlocked.</string>
<string name="AskOverwriteBinary">Do you want to overwrite the existing binary with the same name?</string> <string name="PreloadDatabaseEnabled_title">Pre-load database file</string>
<string name="PreloadDatabaseEnabled_summary">Start background loading or downloading of the database file during password entry.</string>
<string name="AskOverwriteBinary">Do you want to overwrite the existing binary with the same name?</string>
<string name="AskOverwriteBinary_title">Overwrite existing binary?</string> <string name="AskOverwriteBinary_title">Overwrite existing binary?</string>
<string name="AskOverwriteBinary_yes">Overwrite</string> <string name="AskOverwriteBinary_yes">Overwrite</string>
<string name="AskOverwriteBinary_no">Rename</string> <string name="AskOverwriteBinary_no">Rename</string>

View File

@ -163,6 +163,12 @@
android:defaultValue="true" android:defaultValue="true"
android:title="@string/CheckForFileChangesOnSave_title" android:title="@string/CheckForFileChangesOnSave_title"
android:key="@string/CheckForFileChangesOnSave_key" /> android:key="@string/CheckForFileChangesOnSave_key" />
<CheckBoxPreference
android:enabled="true"
android:persistent="true"
android:summary="@string/PreloadDatabaseEnabled_summary"
android:defaultValue="@bool/PreloadDatabaseEnabled_default"
android:title="@string/PreloadDatabaseEnabled_title"
android:key="@string/PreloadDatabaseEnabled_key" />
</PreferenceScreen> </PreferenceScreen>
</PreferenceScreen> </PreferenceScreen>