2013-02-23 11:43:42 -05:00
|
|
|
/*
|
|
|
|
This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll.
|
|
|
|
|
|
|
|
Keepass2Android is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2014-01-26 08:27:27 -05:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
2013-02-23 11:43:42 -05:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Keepass2Android is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using Android.App;
|
|
|
|
using Android.Content;
|
|
|
|
using Android.OS;
|
|
|
|
using Android.Views;
|
|
|
|
using Android.Widget;
|
|
|
|
using Android.Content.PM;
|
|
|
|
using KeePassLib.Keys;
|
|
|
|
using Android.Preferences;
|
|
|
|
using Android.Views.InputMethods;
|
|
|
|
using KeePassLib.Serialization;
|
|
|
|
|
|
|
|
namespace keepass2android
|
|
|
|
{
|
2013-11-11 21:05:19 -05:00
|
|
|
[Activity(Label = "@string/app_name", ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden,
|
|
|
|
Theme = "@style/Base")]
|
2013-02-23 11:43:42 -05:00
|
|
|
public class QuickUnlock : LifecycleDebugActivity
|
|
|
|
{
|
2013-11-11 21:05:19 -05:00
|
|
|
private IOConnectionInfo _ioc;
|
|
|
|
private QuickUnlockBroadcastReceiver _intentReceiver;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2014-02-02 01:03:40 -05:00
|
|
|
private ActivityDesign _design;
|
|
|
|
|
|
|
|
public QuickUnlock()
|
|
|
|
{
|
|
|
|
_design = new ActivityDesign(this);
|
|
|
|
}
|
|
|
|
|
2013-02-23 11:43:42 -05:00
|
|
|
protected override void OnCreate(Bundle bundle)
|
|
|
|
{
|
|
|
|
base.OnCreate(bundle);
|
2014-02-02 01:03:40 -05:00
|
|
|
_design.ApplyTheme();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
_ioc = App.Kp2a.GetDb().Ioc;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if (_ioc == null)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
Finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SetContentView(Resource.Layout.QuickUnlock);
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if (App.Kp2a.GetDb().KpDatabase.Name != "")
|
2013-04-26 04:10:46 -04:00
|
|
|
{
|
|
|
|
FindViewById(Resource.Id.filename_label).Visibility = ViewStates.Invisible;
|
2013-11-11 21:05:19 -05:00
|
|
|
((TextView) FindViewById(Resource.Id.qu_filename)).Text = App.Kp2a.GetDb().KpDatabase.Name;
|
|
|
|
}
|
|
|
|
else
|
2013-04-26 04:10:46 -04:00
|
|
|
{
|
2013-07-27 03:36:32 -04:00
|
|
|
if (
|
|
|
|
PreferenceManager.GetDefaultSharedPreferences(this)
|
2013-11-11 21:05:19 -05:00
|
|
|
.GetBoolean(GetString(Resource.String.RememberRecentFiles_key),
|
|
|
|
Resources.GetBoolean(Resource.Boolean.RememberRecentFiles_default)))
|
2013-07-27 03:36:32 -04:00
|
|
|
{
|
2013-11-11 21:05:19 -05:00
|
|
|
((TextView) FindViewById(Resource.Id.qu_filename)).Text = App.Kp2a.GetFileStorage(_ioc).GetDisplayName(_ioc);
|
2013-07-27 03:36:32 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-11 21:05:19 -05:00
|
|
|
((TextView) FindViewById(Resource.Id.qu_filename)).Text = "*****";
|
2013-07-27 03:36:32 -04:00
|
|
|
}
|
2013-11-11 21:05:19 -05:00
|
|
|
|
2013-04-26 04:10:46 -04:00
|
|
|
}
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
|
2013-11-11 21:05:19 -05:00
|
|
|
TextView txtLabel = (TextView) FindViewById(Resource.Id.QuickUnlock_label);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-07-25 08:47:05 -04:00
|
|
|
int quickUnlockLength = App.Kp2a.QuickUnlockKeyLength;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-11-11 21:05:19 -05:00
|
|
|
txtLabel.Text = GetString(Resource.String.QuickUnlock_label, new Java.Lang.Object[] {quickUnlockLength});
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-11-11 21:05:19 -05:00
|
|
|
EditText pwd = (EditText) FindViewById(Resource.Id.QuickUnlock_password);
|
2013-02-23 11:43:42 -05:00
|
|
|
pwd.SetEms(quickUnlockLength);
|
|
|
|
|
|
|
|
|
2013-11-11 21:05:19 -05:00
|
|
|
Button btnUnlock = (Button) FindViewById(Resource.Id.QuickUnlock_button);
|
|
|
|
btnUnlock.Click += (object sender, EventArgs e) =>
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-11-11 21:05:19 -05:00
|
|
|
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) =>
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-08-07 13:34:43 -04:00
|
|
|
App.Kp2a.LockDatabase(false);
|
2013-11-11 21:05:19 -05:00
|
|
|
Finish();
|
|
|
|
};
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-11-11 21:05:19 -05:00
|
|
|
_intentReceiver = new QuickUnlockBroadcastReceiver(this);
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.AddAction(Intents.DatabaseLocked);
|
|
|
|
RegisterReceiver(_intentReceiver, filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnLockDatabase()
|
|
|
|
{
|
|
|
|
CheckIfUnloaded();
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnResume()
|
|
|
|
{
|
|
|
|
base.OnResume();
|
|
|
|
|
2014-02-02 01:03:40 -05:00
|
|
|
_design.ReapplyTheme();
|
|
|
|
|
2013-11-11 21:05:19 -05:00
|
|
|
CheckIfUnloaded();
|
|
|
|
|
|
|
|
EditText pwd = (EditText) FindViewById(Resource.Id.QuickUnlock_password);
|
2013-08-03 06:33:19 -04:00
|
|
|
pwd.PostDelayed(() =>
|
2013-11-11 21:05:19 -05:00
|
|
|
{
|
|
|
|
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))
|
2013-08-03 06:33:19 -04:00
|
|
|
{
|
2013-11-11 21:05:19 -05:00
|
|
|
Finish();
|
|
|
|
}
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
2013-09-03 15:57:13 -04:00
|
|
|
|
|
|
|
public override void OnBackPressed()
|
|
|
|
{
|
|
|
|
SetResult(KeePass.ExitClose);
|
|
|
|
base.OnBackPressed();
|
|
|
|
}
|
2013-11-11 21:05:19 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|