keepass2android/src/FingerprintTest/MainActivity.cs

152 lines
4.3 KiB
C#
Raw Normal View History

2015-12-29 08:18:09 -05:00
using System;
using Android;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Hardware.Fingerprints;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Preferences;
using Android.Support.V7.App;
using keepass2android;
namespace FingerprintTest
{
2015-12-30 10:08:31 -05:00
[Activity(Label = "FingerprintTest", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/Theme.AppCompat")]
2015-12-29 08:18:09 -05:00
public class MainActivity : AppCompatActivity
{
int count = 1;
2015-12-30 10:08:31 -05:00
private string _keyId = "mykeyid";
2015-12-29 08:18:09 -05:00
const int FINGERPRINT_PERMISSION_REQUEST_CODE = 0;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Visibility = ViewStates.Gone;
RequestPermissions(new[] { Manifest.Permission.UseFingerprint }, FINGERPRINT_PERMISSION_REQUEST_CODE);
}
2015-12-30 10:08:31 -05:00
string prefKey = "enc_pref_key";
2015-12-29 08:18:09 -05:00
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
if (requestCode == FINGERPRINT_PERMISSION_REQUEST_CODE && grantResults[0] == Android.Content.PM.Permission.Granted)
{
2015-12-30 10:08:31 -05:00
Button encButton = FindViewById<Button>(Resource.Id.MyButton);
Button decButton = FindViewById<Button>(Resource.Id.Decrypt);
encButton.Visibility = ViewStates.Visible;
encButton.Enabled = true;
2015-12-29 08:18:09 -05:00
var fingerprint = new keepass2android.FingerprintModule(this);
2015-12-30 10:08:31 -05:00
encButton.Click += (sender, args) =>
{
var fingerprintEnc = new FingerprintEncryption(fingerprint, _keyId);
if (fingerprintEnc.InitCipher())
{
fingerprintEnc.StartListening(new EncryptionCallback(this, fingerprintEnc, prefKey));
}
else
{
Toast.MakeText(this, "Error initiating cipher", ToastLength.Long).Show();
}
};
decButton.Click += (sender, args) =>
2015-12-29 08:18:09 -05:00
{
2015-12-30 10:08:31 -05:00
var fingerprintDec = new FingerprintDecryption(fingerprint, _keyId, this, prefKey);
if (fingerprintDec.InitCipher())
{
fingerprintDec.StartListening(new DecryptionCallback(this, fingerprintDec,prefKey));
}
else
{
Toast.MakeText(this, "Error initiating cipher", ToastLength.Long).Show();
}
2015-12-29 08:18:09 -05:00
};
2015-12-30 10:08:31 -05:00
2015-12-29 08:18:09 -05:00
if (!fingerprint.KeyguardManager.IsKeyguardSecure)
{
2015-12-30 10:08:31 -05:00
encButton.Enabled = false;
2015-12-29 08:18:09 -05:00
// Show a message that the user hasn't set up a fingerprint or lock screen.
Toast.MakeText(this, "Secure lock screen hasn't set up.\n"
+ "Go to 'Settings -> Security -> Fingerprint' to set up a fingerprint", ToastLength.Long).Show();
return;
}
if (!fingerprint.FingerprintManager.HasEnrolledFingerprints)
{
2015-12-30 10:08:31 -05:00
encButton.Enabled = false;
2015-12-29 08:18:09 -05:00
// This happens when no fingerprints are registered.
Toast.MakeText(this, "Go to 'Settings -> Security -> Fingerprint' " +
"and register at least one fingerprint", ToastLength.Long).Show();
return;
}
}
}
}
2015-12-30 10:08:31 -05:00
public class DecryptionCallback : FingerprintManager.AuthenticationCallback
{
private readonly Context _context;
private readonly FingerprintDecryption _fingerprintDec;
private readonly string _prefKey;
public DecryptionCallback(Context context, FingerprintDecryption fingerprintDec, string prefKey)
{
_context = context;
_fingerprintDec = fingerprintDec;
_prefKey = prefKey;
}
public override void OnAuthenticationSucceeded(FingerprintManager.AuthenticationResult result)
{
var prefs = PreferenceManager.GetDefaultSharedPreferences(Application.Context);
Toast.MakeText(_context, _fingerprintDec.DecryptStored(_prefKey, _context), ToastLength.Long).Show();
}
}
2015-12-29 08:18:09 -05:00
public class EncryptionCallback : FingerprintManager.AuthenticationCallback
{
2015-12-30 10:08:31 -05:00
private readonly FingerprintCrypt _fingerprintEnc;
private readonly string _prefKey;
2015-12-29 08:18:09 -05:00
2015-12-30 10:08:31 -05:00
public EncryptionCallback(Context context, FingerprintCrypt fingerprintEnc, string prefKey)
2015-12-29 08:18:09 -05:00
{
_fingerprintEnc = fingerprintEnc;
2015-12-30 10:08:31 -05:00
_prefKey = prefKey;
2015-12-29 08:18:09 -05:00
}
public override void OnAuthenticationSucceeded(FingerprintManager.AuthenticationResult result)
{
2015-12-30 10:08:31 -05:00
_fingerprintEnc.StoreEncrypted("some töst data", _prefKey, Application.Context);
2015-12-29 08:18:09 -05:00
}
2015-12-30 10:08:31 -05:00
2015-12-29 08:18:09 -05:00
}
}