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
{
[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
{
IOConnectionInfo _ioc;
private IOConnectionInfo _ioc;
private QuickUnlockBroadcastReceiver _intentReceiver;
protected override void OnCreate(Bundle bundle)
{
@ -53,7 +55,8 @@ namespace keepass2android
{
FindViewById(Resource.Id.filename_label).Visibility = ViewStates.Invisible;
((TextView) FindViewById(Resource.Id.qu_filename)).Text = App.Kp2a.GetDb().KpDatabase.Name;
} else
}
else
{
if (
PreferenceManager.GetDefaultSharedPreferences(this)
@ -85,7 +88,8 @@ namespace keepass2android
{
KcpPassword kcpPassword = (KcpPassword) App.Kp2a.GetDb().KpDatabase.MasterKey.GetUserKey(typeof (KcpPassword));
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)
{
App.Kp2a.UnlockDatabase();
@ -104,12 +108,24 @@ namespace keepass2android
App.Kp2a.LockDatabase(false);
Finish();
};
_intentReceiver = new QuickUnlockBroadcastReceiver(this);
IntentFilter filter = new IntentFilter();
filter.AddAction(Intents.DatabaseLocked);
RegisterReceiver(_intentReceiver, filter);
}
private void OnLockDatabase()
{
CheckIfUnloaded();
}
protected override void OnResume()
{
base.OnResume();
CheckIfUnloaded();
EditText pwd = (EditText) FindViewById(Resource.Id.QuickUnlock_password);
pwd.PostDelayed(() =>
{
@ -118,11 +134,46 @@ namespace keepass2android
}, 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()
{
SetResult(KeePass.ExitClose);
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
{
[BroadcastReceiver]
[IntentFilter(new[] { Intents.LockDatabase })]
[IntentFilter(new[] { Intents.LockDatabase, Intents.CloseDatabase })]
public class ApplicationBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
@ -19,6 +19,9 @@ namespace keepass2android
case Intents.LockDatabase:
App.Kp2a.LockDatabase();
break;
case Intents.CloseDatabase:
App.Kp2a.LockDatabase(false /*no quick unlock*/);
break;
}
}
}

View File

@ -24,8 +24,10 @@ namespace keepass2android
/// </summary>
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";
/// <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>
public const String DatabaseLocked = "keepass2android.database_locked";

View File

@ -136,8 +136,12 @@ namespace keepass2android
.SetContentTitle(GetString(Resource.String.app_name))
.SetContentText(GetString(Resource.String.database_loaded_quickunlock_enabled, GetDatabaseName()));
var startKp2APendingIntent = GetSwitchToAppPendingIntent();
builder.SetContentIntent(startKp2APendingIntent);
// 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.QuickUnlock_lockButton),
PendingIntent.GetBroadcast(this, 0, new Intent(Intents.CloseDatabase), PendingIntentFlags.UpdateCurrent));
return builder.Build();
}