From f0dfdefd678eddf45917f52983cf2e2cdf9a9b7d Mon Sep 17 00:00:00 2001 From: AlexVallat Date: Sat, 3 Aug 2013 19:58:01 +0100 Subject: [PATCH] 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. --- src/Kp2aBusinessLogic/database/edit/LoadDB.cs | 7 ++-- src/keepass2android/PasswordActivity.cs | 40 +++++++++---------- .../Resources/values/strings.xml | 2 +- .../services/OngoingNotificationsService.cs | 29 +++++++++----- 4 files changed, 43 insertions(+), 35 deletions(-) diff --git a/src/Kp2aBusinessLogic/database/edit/LoadDB.cs b/src/Kp2aBusinessLogic/database/edit/LoadDB.cs index 416239ee..540853de 100644 --- a/src/Kp2aBusinessLogic/database/edit/LoadDB.cs +++ b/src/Kp2aBusinessLogic/database/edit/LoadDB.cs @@ -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 _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 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) { diff --git a/src/keepass2android/PasswordActivity.cs b/src/keepass2android/PasswordActivity.cs index dae84a76..68ce21d8 100644 --- a/src/keepass2android/PasswordActivity.cs +++ b/src/keepass2android/PasswordActivity.cs @@ -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(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(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, ""); diff --git a/src/keepass2android/Resources/values/strings.xml b/src/keepass2android/Resources/values/strings.xml index ac9eee5e..0e0a17de 100644 --- a/src/keepass2android/Resources/values/strings.xml +++ b/src/keepass2android/Resources/values/strings.xml @@ -216,7 +216,7 @@ Add additional string Delete additional string %1$s: Locked. QuickUnlock enabled. - %1$s: Unlocked. Tap to lock. + %1$s: Unlocked. Enter server credentials File transactions Use file transactions for writing databases diff --git a/src/keepass2android/services/OngoingNotificationsService.cs b/src/keepass2android/services/OngoingNotificationsService.cs index d330987e..3981b03b 100644 --- a/src/keepass2android/services/OngoingNotificationsService.cs +++ b/src/keepass2android/services/OngoingNotificationsService.cs @@ -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;