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)
{
@ -52,70 +54,98 @@ namespace keepass2android
if (App.Kp2a.GetDb().KpDatabase.Name != "")
{
FindViewById(Resource.Id.filename_label).Visibility = ViewStates.Invisible;
((TextView)FindViewById(Resource.Id.qu_filename)).Text = App.Kp2a.GetDb().KpDatabase.Name;
} else
((TextView) FindViewById(Resource.Id.qu_filename)).Text = App.Kp2a.GetDb().KpDatabase.Name;
}
else
{
if (
PreferenceManager.GetDefaultSharedPreferences(this)
.GetBoolean(GetString(Resource.String.RememberRecentFiles_key),
Resources.GetBoolean(Resource.Boolean.RememberRecentFiles_default)))
.GetBoolean(GetString(Resource.String.RememberRecentFiles_key),
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
{
((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;
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);
Button btnUnlock = (Button)FindViewById(Resource.Id.QuickUnlock_button);
btnUnlock.Click += (object sender, EventArgs e) =>
{
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));
if (pwd.Text == expectedPasswordPart)
Button btnUnlock = (Button) FindViewById(Resource.Id.QuickUnlock_button);
btnUnlock.Click += (object sender, EventArgs e) =>
{
App.Kp2a.UnlockDatabase();
}
else
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));
if (pwd.Text == expectedPasswordPart)
{
App.Kp2a.UnlockDatabase();
}
else
{
App.Kp2a.LockDatabase(false);
Toast.MakeText(this, GetString(Resource.String.QuickUnlock_fail), ToastLength.Long).Show();
}
Finish();
};
Button btnLock = (Button) FindViewById(Resource.Id.QuickUnlock_buttonLock);
btnLock.Click += (object sender, EventArgs e) =>
{
App.Kp2a.LockDatabase(false);
Toast.MakeText(this, GetString(Resource.String.QuickUnlock_fail), ToastLength.Long).Show();
}
Finish();
};
Finish();
};
Button btnLock = (Button)FindViewById(Resource.Id.QuickUnlock_buttonLock);
btnLock.Click += (object sender, EventArgs e) =>
{
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();
EditText pwd = (EditText)FindViewById(Resource.Id.QuickUnlock_password);
CheckIfUnloaded();
EditText pwd = (EditText) FindViewById(Resource.Id.QuickUnlock_password);
pwd.PostDelayed(() =>
{
InputMethodManager keyboard = (InputMethodManager) GetSystemService(Context.InputMethodService);
keyboard.ShowSoftInput(pwd, 0);
}, 50);
}
protected override void OnDestroy()
{
base.OnDestroy();
UnregisterReceiver(_intentReceiver);
}
private void CheckIfUnloaded()
{
if ((App.Kp2a.GetDb() == null) || (App.Kp2a.GetDb().Loaded == false))
{
InputMethodManager keyboard = (InputMethodManager)GetSystemService(Context.InputMethodService);
keyboard.ShowSoftInput(pwd, 0);
}, 50);
Finish();
}
}
public override void OnBackPressed()
@ -123,6 +153,27 @@ namespace keepass2android
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();
}