Load Database task async wait moved to LoadDb runnable rather than blocking UI thread (in case the file is very slow, or the user is very quick to enter their password)

Database Unlocked warning notification now uses a 4.1 extended "Lock Database" button, and the main action is now just to activate the app.

PasswordActivity no longer loads the database file into memory ready for loading if it's already loaded and we're showing QuickUnlock instead.
This commit is contained in:
AlexVallat 2013-08-03 19:58:01 +01:00
parent 84efaae462
commit f0dfdefd67
4 changed files with 43 additions and 35 deletions

View File

@ -17,19 +17,20 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
using System;
using System.IO;
using System.Threading.Tasks;
using KeePassLib.Serialization;
namespace keepass2android
{
public class LoadDb : RunnableOnFinish {
private readonly IOConnectionInfo _ioc;
private readonly MemoryStream _databaseData;
private readonly Task<MemoryStream> _databaseData;
private readonly String _pass;
private readonly String _key;
private readonly IKp2aApp _app;
private readonly bool _rememberKeyfile;
public LoadDb(IKp2aApp app, IOConnectionInfo ioc, MemoryStream databaseData, String pass, String key, OnFinish finish): base(finish)
public LoadDb(IKp2aApp app, IOConnectionInfo ioc, Task<MemoryStream> databaseData, String pass, String key, OnFinish finish): base(finish)
{
_app = app;
_ioc = ioc;
@ -47,7 +48,7 @@ namespace keepass2android
try
{
StatusLogger.UpdateMessage(UiStringKey.loading_database);
_app.LoadDatabase(_ioc, _databaseData, _pass, _key, StatusLogger);
_app.LoadDatabase(_ioc, _databaseData.Result, _pass, _key, StatusLogger);
SaveFileData (_ioc, _key);
} catch (KeyFileException) {

View File

@ -128,35 +128,19 @@ namespace keepass2android
public void LaunchNextActivity()
{
AppTask.AfterUnlockDatabase(this);
}
void TryStartQuickUnlock()
{
if (App.Kp2a.QuickUnlockEnabled && App.Kp2a.QuickLocked)
{
Intent i = new Intent(this, typeof(QuickUnlock));
PutIoConnectionToIntent(_ioConnection, i);
Kp2aLog.Log("Starting QuickUnlock");
StartActivityForResult(i, 0);
}
}
bool _startedWithActivityResult;
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
_startedWithActivityResult = true;
Kp2aLog.Log("PasswordActivity.OnActivityResult "+resultCode+"/"+requestCode);
//NOTE: original code from k eepassdroid used switch ((Android.App.Result)requestCode) { (but doesn't work here, although k eepassdroid works)
switch(resultCode) {
case KeePass.ExitLock:
// The database has already been locked, just show the quick unlock screen if appropriate
TryStartQuickUnlock();
// The database has already been locked, and the quick unlock screen will be shown if appropriate
break;
case KeePass.ExitForceLock:
App.Kp2a.LockDatabase(false);
@ -300,9 +284,9 @@ namespace keepass2android
App.Kp2a.SetQuickUnlockEnabled(cbQuickUnlock.Checked);
Handler handler = new Handler();
var stream = _loadDbTask.Result;
LoadDb task = new LoadDb(App.Kp2a, _ioConnection, _loadDbTask, pass, key, new AfterLoad(handler, this));
_loadDbTask = null; // prevent accidental re-use
LoadDb task = new LoadDb(App.Kp2a, _ioConnection, stream, pass, key, new AfterLoad(handler, this));
ProgressTask pt = new ProgressTask(App.Kp2a, this, task);
pt.Run();
};
@ -382,8 +366,18 @@ namespace keepass2android
{
base.OnStart();
// Create task to kick off file loading while the user enters the password
_loadDbTask = Task.Factory.StartNew<MemoryStream>(LoadDbFile);
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()
@ -407,6 +401,8 @@ namespace keepass2android
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
}
System.Threading.Thread.Sleep(3000);
Kp2aLog.Log("Pre-loading database file completed");
return memoryStream;
@ -418,6 +414,7 @@ namespace keepass2android
AppTask.ToBundle(outState);
}
/*
protected override void OnResume() {
base.OnResume();
@ -437,6 +434,7 @@ namespace keepass2android
}
}
}
* */
private void RetrieveSettings() {
String defaultFilename = _prefs.GetString(KeyDefaultFilename, "");

View File

@ -216,7 +216,7 @@
<string name="add_extra_string">Add additional string</string>
<string name="delete_extra_string">Delete additional string</string>
<string name="database_loaded_quickunlock_enabled">%1$s: Locked. QuickUnlock enabled.</string>
<string name="database_loaded_unlocked">%1$s: Unlocked. Tap to lock.</string>
<string name="database_loaded_unlocked">%1$s: Unlocked.</string>
<string name="credentials_dialog_title">Enter server credentials</string>
<string name="UseFileTransactions_title">File transactions</string>
<string name="UseFileTransactions_summary">Use file transactions for writing databases</string>

View File

@ -128,22 +128,19 @@ namespace keepass2android
new NotificationCompat.Builder(this)
.SetSmallIcon(Resource.Drawable.ic_launcher_gray)
.SetLargeIcon(BitmapFactory.DecodeResource(Resources, AppNames.LauncherIcon))
.SetContentTitle(GetText(Resource.String.app_name))
.SetContentTitle(GetString(Resource.String.app_name))
.SetContentText(GetString(Resource.String.database_loaded_quickunlock_enabled, GetDatabaseName()));
Intent startKp2aIntent = new Intent(this, typeof(KeePass));
startKp2aIntent.SetAction(Intent.ActionMain);
startKp2aIntent.AddCategory(Intent.CategoryLauncher);
PendingIntent startKp2APendingIntent =
PendingIntent.GetActivity(this, 0, startKp2aIntent, PendingIntentFlags.UpdateCurrent);
var startKp2APendingIntent = GetSwitchToAppPendingIntent();
builder.SetContentIntent(startKp2APendingIntent);
return builder.Build();
}
#endregion
#region Unlocked Warning
private Notification GetUnlockedNotification()
{
NotificationCompat.Builder builder =
@ -151,14 +148,26 @@ namespace keepass2android
.SetOngoing(true)
.SetSmallIcon(Resource.Drawable.ic_unlocked_gray)
.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.ic_launcher_red))
.SetContentTitle(GetText(Resource.String.app_name))
.SetContentTitle(GetString(Resource.String.app_name))
.SetContentText(GetString(Resource.String.database_loaded_unlocked, GetDatabaseName()));
builder.SetContentIntent(PendingIntent.GetBroadcast(this, 0, new Intent(Intents.LockDatabase), PendingIntentFlags.UpdateCurrent));
// Default action is to show Kp2A
builder.SetContentIntent(GetSwitchToAppPendingIntent());
// Additional action to allow locking the database
builder.AddAction(Android.Resource.Drawable.IcLockLock, GetString(Resource.String.menu_lock), PendingIntent.GetBroadcast(this, 0, new Intent(Intents.LockDatabase), PendingIntentFlags.UpdateCurrent));
return builder.Build();
}
private PendingIntent GetSwitchToAppPendingIntent()
{
var startKp2aIntent = new Intent(this, typeof(KeePass));
startKp2aIntent.SetAction(Intent.ActionMain);
startKp2aIntent.AddCategory(Intent.CategoryLauncher);
return PendingIntent.GetActivity(this, 0, startKp2aIntent, PendingIntentFlags.UpdateCurrent);
}
private static string GetDatabaseName()
{
var db = App.Kp2a.GetDb().KpDatabase;