added "close database" action button for QuickUnlock notification

This commit is contained in:
Philipp Crocoll 2013-11-12 03:05:19 +01:00
parent 574a56c2e3
commit 0e5c313014
4 changed files with 100 additions and 40 deletions

View File

@ -29,10 +29,12 @@ using KeePassLib.Serialization;
namespace keepass2android namespace keepass2android
{ {
[Activity (Label = "@string/app_name", ConfigurationChanges=ConfigChanges.Orientation|ConfigChanges.KeyboardHidden, Theme="@style/Base")] [Activity(Label = "@string/app_name", ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden,
Theme = "@style/Base")]
public class QuickUnlock : LifecycleDebugActivity public class QuickUnlock : LifecycleDebugActivity
{ {
IOConnectionInfo _ioc; private IOConnectionInfo _ioc;
private QuickUnlockBroadcastReceiver _intentReceiver;
protected override void OnCreate(Bundle bundle) protected override void OnCreate(Bundle bundle)
{ {
@ -52,40 +54,42 @@ namespace keepass2android
if (App.Kp2a.GetDb().KpDatabase.Name != "") if (App.Kp2a.GetDb().KpDatabase.Name != "")
{ {
FindViewById(Resource.Id.filename_label).Visibility = ViewStates.Invisible; FindViewById(Resource.Id.filename_label).Visibility = ViewStates.Invisible;
((TextView)FindViewById(Resource.Id.qu_filename)).Text = App.Kp2a.GetDb().KpDatabase.Name; ((TextView) FindViewById(Resource.Id.qu_filename)).Text = App.Kp2a.GetDb().KpDatabase.Name;
} else }
else
{ {
if ( if (
PreferenceManager.GetDefaultSharedPreferences(this) PreferenceManager.GetDefaultSharedPreferences(this)
.GetBoolean(GetString(Resource.String.RememberRecentFiles_key), .GetBoolean(GetString(Resource.String.RememberRecentFiles_key),
Resources.GetBoolean(Resource.Boolean.RememberRecentFiles_default))) Resources.GetBoolean(Resource.Boolean.RememberRecentFiles_default)))
{ {
((TextView)FindViewById(Resource.Id.qu_filename)).Text = App.Kp2a.GetFileStorage(_ioc).GetDisplayName(_ioc); ((TextView) FindViewById(Resource.Id.qu_filename)).Text = App.Kp2a.GetFileStorage(_ioc).GetDisplayName(_ioc);
} }
else else
{ {
((TextView)FindViewById(Resource.Id.qu_filename)).Text = "*****"; ((TextView) FindViewById(Resource.Id.qu_filename)).Text = "*****";
} }
} }
TextView txtLabel = (TextView)FindViewById(Resource.Id.QuickUnlock_label); TextView txtLabel = (TextView) FindViewById(Resource.Id.QuickUnlock_label);
int quickUnlockLength = App.Kp2a.QuickUnlockKeyLength; int quickUnlockLength = App.Kp2a.QuickUnlockKeyLength;
txtLabel.Text = GetString(Resource.String.QuickUnlock_label, new Java.Lang.Object[]{quickUnlockLength}); txtLabel.Text = GetString(Resource.String.QuickUnlock_label, new Java.Lang.Object[] {quickUnlockLength});
EditText pwd= (EditText)FindViewById(Resource.Id.QuickUnlock_password); EditText pwd = (EditText) FindViewById(Resource.Id.QuickUnlock_password);
pwd.SetEms(quickUnlockLength); pwd.SetEms(quickUnlockLength);
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) =>
{ {
KcpPassword kcpPassword = (KcpPassword)App.Kp2a.GetDb().KpDatabase.MasterKey.GetUserKey(typeof(KcpPassword)); KcpPassword kcpPassword = (KcpPassword) App.Kp2a.GetDb().KpDatabase.MasterKey.GetUserKey(typeof (KcpPassword));
String password = kcpPassword.Password.ReadString(); String password = kcpPassword.Password.ReadString();
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)
{ {
App.Kp2a.UnlockDatabase(); App.Kp2a.UnlockDatabase();
@ -98,31 +102,78 @@ namespace keepass2android
Finish(); Finish();
}; };
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) =>
{ {
App.Kp2a.LockDatabase(false); App.Kp2a.LockDatabase(false);
Finish(); Finish();
}; };
_intentReceiver = new QuickUnlockBroadcastReceiver(this);
IntentFilter filter = new IntentFilter();
filter.AddAction(Intents.DatabaseLocked);
RegisterReceiver(_intentReceiver, filter);
}
private void OnLockDatabase()
{
CheckIfUnloaded();
} }
protected override void OnResume() protected override void OnResume()
{ {
base.OnResume(); base.OnResume();
EditText pwd = (EditText)FindViewById(Resource.Id.QuickUnlock_password); CheckIfUnloaded();
EditText pwd = (EditText) FindViewById(Resource.Id.QuickUnlock_password);
pwd.PostDelayed(() => pwd.PostDelayed(() =>
{ {
InputMethodManager keyboard = (InputMethodManager)GetSystemService(Context.InputMethodService); InputMethodManager keyboard = (InputMethodManager) GetSystemService(Context.InputMethodService);
keyboard.ShowSoftInput(pwd, 0); keyboard.ShowSoftInput(pwd, 0);
}, 50); }, 50);
} }
protected override void OnDestroy()
{
base.OnDestroy();
UnregisterReceiver(_intentReceiver);
}
private void CheckIfUnloaded()
{
if ((App.Kp2a.GetDb() == null) || (App.Kp2a.GetDb().Loaded == false))
{
Finish();
}
}
public override void OnBackPressed() public override void OnBackPressed()
{ {
SetResult(KeePass.ExitClose); SetResult(KeePass.ExitClose);
base.OnBackPressed(); base.OnBackPressed();
} }
private class QuickUnlockBroadcastReceiver : BroadcastReceiver
{
readonly QuickUnlock _activity;
public QuickUnlockBroadcastReceiver(QuickUnlock activity)
{
_activity = activity;
}
public override void OnReceive(Context context, Intent intent)
{
switch (intent.Action)
{
case Intents.DatabaseLocked:
_activity.OnLockDatabase();
break;
}
}
}
} }
} }

View File

@ -7,7 +7,7 @@ using Android.App;
namespace keepass2android namespace keepass2android
{ {
[BroadcastReceiver] [BroadcastReceiver]
[IntentFilter(new[] { Intents.LockDatabase })] [IntentFilter(new[] { Intents.LockDatabase, Intents.CloseDatabase })]
public class ApplicationBroadcastReceiver : BroadcastReceiver public class ApplicationBroadcastReceiver : BroadcastReceiver
{ {
public override void OnReceive(Context context, Intent intent) public override void OnReceive(Context context, Intent intent)
@ -19,6 +19,9 @@ namespace keepass2android
case Intents.LockDatabase: case Intents.LockDatabase:
App.Kp2a.LockDatabase(); App.Kp2a.LockDatabase();
break; break;
case Intents.CloseDatabase:
App.Kp2a.LockDatabase(false /*no quick unlock*/);
break;
} }
} }
} }

View File

@ -24,8 +24,10 @@ namespace keepass2android
/// </summary> /// </summary>
public class Intents public class Intents
{ {
/// <summary>Broadcast this intent to lock the database</summary> /// <summary>Broadcast this intent to lock the database (with quick unlock if enabled)</summary>
public const String LockDatabase = "keepass2android.lock_database"; public const String LockDatabase = "keepass2android.lock_database";
/// <summary>Broadcast this intent to close the database (no quick unlock, full close)</summary>
public const String CloseDatabase = "keepass2android.close_database";
/// <summary>This intent will be broadcast once the database has been locked. Sensitive information displayed should be hidden and unloaded.</summary> /// <summary>This intent will be broadcast once the database has been locked. Sensitive information displayed should be hidden and unloaded.</summary>
public const String DatabaseLocked = "keepass2android.database_locked"; public const String DatabaseLocked = "keepass2android.database_locked";

View File

@ -136,8 +136,12 @@ namespace keepass2android
.SetContentTitle(GetString(Resource.String.app_name)) .SetContentTitle(GetString(Resource.String.app_name))
.SetContentText(GetString(Resource.String.database_loaded_quickunlock_enabled, GetDatabaseName())); .SetContentText(GetString(Resource.String.database_loaded_quickunlock_enabled, GetDatabaseName()));
var startKp2APendingIntent = GetSwitchToAppPendingIntent(); // Default action is to show Kp2A
builder.SetContentIntent(startKp2APendingIntent); builder.SetContentIntent(GetSwitchToAppPendingIntent());
// Additional action to allow locking the database
builder.AddAction(Android.Resource.Drawable.IcLockLock, GetString(Resource.String.QuickUnlock_lockButton),
PendingIntent.GetBroadcast(this, 0, new Intent(Intents.CloseDatabase), PendingIntentFlags.UpdateCurrent));
return builder.Build(); return builder.Build();
} }