add button to scan QR code for TOTP setup in the "setup TOTP" dialog. Closes https://github.com/PhilippC/keepass2android/issues/1575

This commit is contained in:
Philipp Crocoll
2022-01-15 20:10:34 +01:00
parent 2b85ed473c
commit c344b0f62a
5 changed files with 68 additions and 6 deletions

View File

@@ -39,11 +39,15 @@ using Android.Content.Res;
using Android.Database;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Runtime;
using Android.Util;
using keepass2android.Io;
using KeePassLib.Serialization;
using KeeTrayTOTP.Libraries;
using PluginTOTP;
using Xamarin.Essentials;
using Xamarin.Forms.Platform.Android;
using ZXing.Mobile;
using Debug = System.Diagnostics.Debug;
using File = System.IO.File;
using Object = Java.Lang.Object;
@@ -1113,8 +1117,8 @@ namespace keepass2android
private string[] _additionalKeys = null;
private List<View> _editModeHiddenViews;
private Uri _uriToAddOrAsk;
public string[] AdditionalKeys
public string[] AdditionalKeys
{
get
{
@@ -1177,6 +1181,25 @@ namespace keepass2android
dlgView.FindViewById(Resource.Id.totp_custom_settings_group).Visibility = args.IsChecked ? ViewStates.Visible : ViewStates.Gone;
};
dlgView.FindViewById<Button>(Resource.Id.totp_scan).Click += async (object o, EventArgs args) =>
{
var scanner = new ZXing.Mobile.MobileBarcodeScanner();
var options = new ZXing.Mobile.MobileBarcodeScanningOptions();
options.PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.QR_CODE };
var result = await scanner.Scan(options);
if (result?.Text?.StartsWith("otpauth://") == true)
{
dialog.Dismiss();
var targetField = ((TextView)((View)sender.Parent).FindViewById(Resource.Id.value));
targetField.Text = result.Text;
}
else
{
Toast.MakeText(this, "Scanned code should contain an otpauth:// text.", ToastLength.Long).Show();
}
};
//copy values from entry into dialog
View ees = (View)sender.Parent;
@@ -1208,14 +1231,22 @@ namespace keepass2android
_passwordFont.ApplyTo(dlgView.FindViewById<EditText>(Resource.Id.totp_secret_key));
Util.SetNoPersonalizedLearning(dlgView);
dialog.Show();
}
string SanitizeInput(string encodedData)
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
string SanitizeInput(string encodedData)
{
if (encodedData.Length <= 0)
{

View File

@@ -63,6 +63,14 @@
android:hint="@string/totp_length"
android:id="@+id/totp_length" />
<Button
android:drawableLeft="@android:drawable/ic_menu_camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/totp_scan"
android:id="@+id/totp_scan"
/>
</LinearLayout>

View File

@@ -386,6 +386,7 @@
<string name="totp_encoding_custom">Custom token settings</string>
<string name="totp_time_step">Time step</string>
<string name="totp_length">Code length</string>
<string name="totp_scan">Scan QR code</string>
<string name="delete_extra_string">Delete additional string</string>
<string name="database_loaded_quickunlock_enabled">%1$s: Locked. QuickUnlock enabled.</string>

View File

@@ -1230,7 +1230,11 @@ namespace keepass2android
intentFilter.AddAction(Intents.LockDatabaseByTimeout);
intentFilter.AddAction(Intents.CloseDatabase);
Context.RegisterReceiver(broadcastReceiver, intentFilter);
}
Xamarin.Essentials.Platform.Init(this);
ZXing.Net.Mobile.Forms.Android.Platform.Init();
}
private ApplicationBroadcastReceiver broadcastReceiver = new ApplicationBroadcastReceiver();

View File

@@ -68,6 +68,24 @@
<BundleAssemblies>false</BundleAssemblies>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Xamarin.AndroidX.Core">
<Version>1.5.0</Version>
</PackageReference>
<PackageReference Include="Xamarin.AndroidX.CustomView">
<Version>1.1.0.9</Version>
</PackageReference>
<PackageReference Include="Xamarin.AndroidX.Preference">
<Version>1.1.1.11</Version>
</PackageReference>
<PackageReference Include="Xamarin.AndroidX.RecyclerView">
<Version>1.2.1.3</Version>
</PackageReference>
<PackageReference Include="ZXing.Net.Mobile">
<Version>3.1.0-beta2</Version>
</PackageReference>
<PackageReference Include="ZXing.Net.Mobile.Forms">
<Version>3.1.0-beta2</Version>
</PackageReference>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />