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;
|
2016-01-02 15:29:26 -05:00
|
|
|
|
using Android;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
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;
|
2015-09-15 15:45:04 -04:00
|
|
|
|
using Android.Runtime;
|
|
|
|
|
using Android.Support.Design.Widget;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
using Android.Views.InputMethods;
|
|
|
|
|
using KeePassLib.Serialization;
|
|
|
|
|
|
|
|
|
|
namespace keepass2android
|
|
|
|
|
{
|
2015-09-15 15:45:04 -04:00
|
|
|
|
[Activity(Label = "@string/app_name",
|
|
|
|
|
ConfigurationChanges = ConfigChanges.Orientation,
|
|
|
|
|
WindowSoftInputMode = SoftInput.AdjustResize,
|
|
|
|
|
MainLauncher = false,
|
2015-09-08 13:58:20 -04:00
|
|
|
|
Theme = "@style/MyTheme_Blue")]
|
2016-01-02 15:29:26 -05:00
|
|
|
|
public class QuickUnlock : LifecycleDebugActivity, IFingerprintAuthCallback
|
2013-02-23 11:43:42 -05:00
|
|
|
|
{
|
2013-11-11 21:05:19 -05:00
|
|
|
|
private IOConnectionInfo _ioc;
|
|
|
|
|
private QuickUnlockBroadcastReceiver _intentReceiver;
|
2015-12-22 07:23:21 -05:00
|
|
|
|
private ActivityDesign _design;
|
2016-01-02 15:29:26 -05:00
|
|
|
|
private bool _fingerprintPermissionGranted;
|
|
|
|
|
private FingerprintDecryption _fingerprintDec;
|
|
|
|
|
private int _quickUnlockLength;
|
|
|
|
|
private const int FingerprintPermissionRequestCode = 0;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
2014-02-02 01:03:40 -05:00
|
|
|
|
public QuickUnlock()
|
|
|
|
|
{
|
2015-12-22 07:23:21 -05:00
|
|
|
|
_design = new ActivityDesign(this);
|
2014-02-02 01:03:40 -05:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-23 11:43:42 -05:00
|
|
|
|
protected override void OnCreate(Bundle bundle)
|
|
|
|
|
{
|
2015-12-22 07:23:21 -05:00
|
|
|
|
_design.ApplyTheme();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
base.OnCreate(bundle);
|
2015-09-15 15:45:04 -04:00
|
|
|
|
|
2015-02-11 02:41:59 -05:00
|
|
|
|
//use FlagSecure to make sure the last (revealed) character of the password is not visible in recent apps
|
|
|
|
|
if (PreferenceManager.GetDefaultSharedPreferences(this).GetBoolean(
|
|
|
|
|
GetString(Resource.String.ViewDatabaseSecure_key), true))
|
|
|
|
|
{
|
|
|
|
|
Window.SetFlags(WindowManagerFlags.Secure, WindowManagerFlags.Secure);
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
2015-10-29 15:31:34 -04:00
|
|
|
|
|
|
|
|
|
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.mytoolbar);
|
|
|
|
|
|
|
|
|
|
SetSupportActionBar(toolbar);
|
|
|
|
|
|
|
|
|
|
var collapsingToolbar = FindViewById<CollapsingToolbarLayout>(Resource.Id.collapsing_toolbar);
|
|
|
|
|
collapsingToolbar.SetTitle(GetString(Resource.String.QuickUnlock_prefs));
|
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
|
if (App.Kp2a.GetDb().KpDatabase.Name != "")
|
2013-04-26 04:10:46 -04:00
|
|
|
|
{
|
2015-08-19 01:03:12 -04:00
|
|
|
|
FindViewById(Resource.Id.filename_label).Visibility = ViewStates.Visible;
|
|
|
|
|
((TextView) FindViewById(Resource.Id.filename_label)).Text = App.Kp2a.GetDb().KpDatabase.Name;
|
2013-11-11 21:05:19 -05:00
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2015-08-19 01:03:12 -04:00
|
|
|
|
((TextView) FindViewById(Resource.Id.filename_label)).Text = App.Kp2a.GetFileStorage(_ioc).GetDisplayName(_ioc);
|
2013-07-27 03:36:32 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-08-19 01:03:12 -04:00
|
|
|
|
((TextView) FindViewById(Resource.Id.filename_label)).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
|
|
|
|
|
2016-01-02 15:29:26 -05:00
|
|
|
|
_quickUnlockLength = App.Kp2a.QuickUnlockKeyLength;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
2016-01-02 15:29:26 -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);
|
2016-01-02 15:29:26 -05:00
|
|
|
|
pwd.SetEms(_quickUnlockLength);
|
2016-01-04 09:34:58 -05:00
|
|
|
|
Util.MoveBottomBarButtons(Resource.Id.QuickUnlock_buttonLock, Resource.Id.QuickUnlock_button, Resource.Id.bottom_bar, this);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
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
|
|
|
|
{
|
2016-01-02 15:29:26 -05:00
|
|
|
|
OnUnlock(_quickUnlockLength, pwd);
|
2013-11-11 21:05:19 -05:00
|
|
|
|
};
|
|
|
|
|
|
2015-09-04 02:12:32 -04:00
|
|
|
|
|
2015-08-10 09:39:34 -04:00
|
|
|
|
|
2013-11-11 21:05:19 -05:00
|
|
|
|
Button btnLock = (Button) FindViewById(Resource.Id.QuickUnlock_buttonLock);
|
2015-10-31 00:02:44 -04:00
|
|
|
|
btnLock.Text = btnLock.Text.Replace("<22>", "ss");
|
2013-11-11 21:05:19 -05:00
|
|
|
|
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();
|
|
|
|
|
};
|
2015-02-17 00:05:48 -05:00
|
|
|
|
pwd.EditorAction += (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
if ((args.ActionId == ImeAction.Done) || ((args.ActionId == ImeAction.ImeNull) && (args.Event.Action == KeyEventActions.Down)))
|
2016-01-02 15:29:26 -05:00
|
|
|
|
OnUnlock(_quickUnlockLength, pwd);
|
2015-02-17 00:05:48 -05:00
|
|
|
|
};
|
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);
|
2016-01-02 15:29:26 -05:00
|
|
|
|
|
|
|
|
|
if ((int)Build.VERSION.SdkInt >= 23)
|
|
|
|
|
RequestPermissions(new[] { Manifest.Permission.UseFingerprint }, FingerprintPermissionRequestCode);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
|
|
|
|
|
{
|
|
|
|
|
if (requestCode == FingerprintPermissionRequestCode && grantResults[0] == Permission.Granted)
|
|
|
|
|
{
|
|
|
|
|
var btn = FindViewById<ImageButton>(Resource.Id.fingerprintbtn);
|
|
|
|
|
btn.Click += (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
AlertDialog.Builder b = new AlertDialog.Builder(this);
|
|
|
|
|
b.SetTitle(Resource.String.fingerprint_prefs);
|
|
|
|
|
b.SetMessage(btn.Tag.ToString());
|
|
|
|
|
b.SetPositiveButton(Android.Resource.String.Ok, (o, eventArgs) => ((Dialog)o).Dismiss());
|
|
|
|
|
b.Show();
|
|
|
|
|
};
|
|
|
|
|
_fingerprintPermissionGranted = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnFingerprintError(string message)
|
|
|
|
|
{
|
|
|
|
|
var btn = FindViewById<ImageButton>(Resource.Id.fingerprintbtn);
|
|
|
|
|
|
|
|
|
|
btn.SetImageResource(Resource.Drawable.ic_fingerprint_error);
|
|
|
|
|
btn.PostDelayed(() =>
|
|
|
|
|
{
|
|
|
|
|
btn.SetImageResource(Resource.Drawable.ic_fp_40px);
|
|
|
|
|
btn.Tag = GetString(Resource.String.fingerprint_unlock_hint);
|
|
|
|
|
}, 1300);
|
|
|
|
|
Toast.MakeText(this, message, ToastLength.Long).Show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnFingerprintAuthSucceeded()
|
|
|
|
|
{
|
2016-01-04 09:34:58 -05:00
|
|
|
|
_fingerprintDec.StopListening();
|
2016-01-02 15:29:26 -05:00
|
|
|
|
var btn = FindViewById<ImageButton>(Resource.Id.fingerprintbtn);
|
|
|
|
|
|
|
|
|
|
btn.SetImageResource(Resource.Drawable.ic_fingerprint_success);
|
|
|
|
|
|
|
|
|
|
EditText pwd = (EditText)FindViewById(Resource.Id.QuickUnlock_password);
|
|
|
|
|
pwd.Text = ExpectedPasswordPart;
|
2016-01-04 09:34:58 -05:00
|
|
|
|
|
2016-01-02 15:29:26 -05:00
|
|
|
|
btn.PostDelayed(() =>
|
|
|
|
|
{
|
2015-09-15 15:45:04 -04:00
|
|
|
|
|
2016-01-02 15:29:26 -05:00
|
|
|
|
App.Kp2a.UnlockDatabase();
|
|
|
|
|
Finish();
|
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
|
|
|
2013-11-11 21:05:19 -05:00
|
|
|
|
}
|
2016-01-02 15:29:26 -05:00
|
|
|
|
private void InitFingerprintUnlock()
|
|
|
|
|
{
|
|
|
|
|
var btn = FindViewById<ImageButton>(Resource.Id.fingerprintbtn);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
FingerprintUnlockMode um;
|
|
|
|
|
Enum.TryParse(PreferenceManager.GetDefaultSharedPreferences(this).GetString(App.Kp2a.GetDb().CurrentFingerprintModePrefKey, ""), out um);
|
2016-01-04 09:34:58 -05:00
|
|
|
|
btn.Visibility = (um != FingerprintUnlockMode.Disabled) ? ViewStates.Visible : ViewStates.Gone;
|
2016-01-02 15:29:26 -05:00
|
|
|
|
|
2016-01-04 09:34:58 -05:00
|
|
|
|
if (um == FingerprintUnlockMode.Disabled)
|
2016-01-02 15:29:26 -05:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FingerprintModule fpModule = new FingerprintModule(this);
|
|
|
|
|
_fingerprintDec = new FingerprintDecryption(fpModule, App.Kp2a.GetDb().CurrentFingerprintPrefKey, this,
|
|
|
|
|
App.Kp2a.GetDb().CurrentFingerprintPrefKey);
|
2013-11-11 21:05:19 -05:00
|
|
|
|
|
2016-01-02 15:29:26 -05:00
|
|
|
|
btn.Tag = GetString(Resource.String.fingerprint_unlock_hint);
|
|
|
|
|
|
|
|
|
|
if (_fingerprintDec.InitCipher())
|
|
|
|
|
{
|
|
|
|
|
btn.SetImageResource(Resource.Drawable.ic_fp_40px);
|
|
|
|
|
_fingerprintDec.StartListening(new FingerprintAuthCallbackAdapter(this, this));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//key invalidated permanently
|
|
|
|
|
btn.SetImageResource(Resource.Drawable.ic_fingerprint_error);
|
|
|
|
|
btn.Tag = GetString(Resource.String.fingerprint_unlock_failed);
|
|
|
|
|
_fingerprintDec = null;
|
|
|
|
|
|
|
|
|
|
ClearFingerprintUnlockData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
btn.SetImageResource(Resource.Drawable.ic_fingerprint_error);
|
|
|
|
|
btn.Tag = "Error initializing Fingerprint Unlock: " + e;
|
|
|
|
|
|
|
|
|
|
_fingerprintDec = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ClearFingerprintUnlockData()
|
|
|
|
|
{
|
|
|
|
|
ISharedPreferencesEditor edit = PreferenceManager.GetDefaultSharedPreferences(this).Edit();
|
|
|
|
|
edit.PutString(App.Kp2a.GetDb().CurrentFingerprintPrefKey, "");
|
|
|
|
|
edit.PutString(App.Kp2a.GetDb().CurrentFingerprintModePrefKey, FingerprintUnlockMode.Disabled.ToString());
|
|
|
|
|
edit.Commit();
|
|
|
|
|
}
|
2015-09-15 15:45:04 -04:00
|
|
|
|
|
2015-02-17 00:05:48 -05:00
|
|
|
|
private void OnUnlock(int quickUnlockLength, EditText pwd)
|
|
|
|
|
{
|
2016-01-02 15:29:26 -05:00
|
|
|
|
var expectedPasswordPart = ExpectedPasswordPart;
|
2015-02-17 00:05:48 -05:00
|
|
|
|
if (pwd.Text == expectedPasswordPart)
|
|
|
|
|
{
|
|
|
|
|
App.Kp2a.UnlockDatabase();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
App.Kp2a.LockDatabase(false);
|
|
|
|
|
Toast.MakeText(this, GetString(Resource.String.QuickUnlock_fail), ToastLength.Long).Show();
|
|
|
|
|
}
|
|
|
|
|
Finish();
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-02 15:29:26 -05:00
|
|
|
|
private string ExpectedPasswordPart
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
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));
|
|
|
|
|
return expectedPasswordPart;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-11 21:05:19 -05:00
|
|
|
|
private void OnLockDatabase()
|
|
|
|
|
{
|
|
|
|
|
CheckIfUnloaded();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnResume()
|
|
|
|
|
{
|
|
|
|
|
base.OnResume();
|
2015-12-22 07:23:21 -05:00
|
|
|
|
_design.ReapplyTheme();
|
2015-09-15 15:45:04 -04:00
|
|
|
|
|
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);
|
2016-01-02 15:29:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (_fingerprintPermissionGranted)
|
|
|
|
|
{
|
|
|
|
|
InitFingerprintUnlock();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FindViewById<ImageButton>(Resource.Id.fingerprintbtn).Visibility = ViewStates.Gone;
|
|
|
|
|
|
|
|
|
|
}
|
2013-11-11 21:05:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
base.OnDestroy();
|
2014-02-02 18:30:21 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
UnregisterReceiver(_intentReceiver);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Kp2aLog.Log(e.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-11 21:05:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|