bitwarden-android/src/App/Pages/Vault/ScanPage.xaml.cs

92 lines
2.8 KiB
C#
Raw Normal View History

2019-05-23 03:54:17 +03:00
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace Bit.App.Pages
{
public partial class ScanPage : BaseContentPage
{
private readonly Action<string> _callback;
private DateTime? _timerStarted = null;
private TimeSpan _timerMaxLength = TimeSpan.FromMinutes(3);
public ScanPage(Action<string> callback)
{
_callback = callback;
2019-05-23 04:10:04 +03:00
InitializeComponent();
2019-05-23 03:54:17 +03:00
_zxing.Options = new ZXing.Mobile.MobileBarcodeScanningOptions
{
UseNativeScanning = true,
PossibleFormats = new List<ZXing.BarcodeFormat> { ZXing.BarcodeFormat.QR_CODE },
AutoRotate = false,
};
2019-06-12 04:31:51 +03:00
if(Device.RuntimePlatform == Device.Android)
{
ToolbarItems.RemoveAt(0);
}
2019-05-23 03:54:17 +03:00
}
protected override void OnAppearing()
{
base.OnAppearing();
_zxing.IsScanning = true;
_timerStarted = DateTime.Now;
Device.StartTimer(new TimeSpan(0, 0, 2), () =>
{
if(_timerStarted == null || (DateTime.Now - _timerStarted) > _timerMaxLength)
{
return false;
}
_zxing.AutoFocus();
return true;
});
}
protected override void OnDisappearing()
{
_timerStarted = null;
_zxing.IsScanning = false;
base.OnDisappearing();
}
private void OnScanResult(ZXing.Result result)
{
// Stop analysis until we navigate away so we don't keep reading barcodes
_zxing.IsAnalyzing = false;
_zxing.IsScanning = false;
2019-06-05 07:41:12 +03:00
var text = result?.Text;
if(!string.IsNullOrWhiteSpace(text))
2019-05-23 03:54:17 +03:00
{
2019-06-05 07:41:12 +03:00
if(text.StartsWith("otpauth://totp"))
2019-05-23 03:54:17 +03:00
{
2019-06-05 07:41:12 +03:00
_callback(text);
return;
}
else if(Uri.TryCreate(text, UriKind.Absolute, out Uri uri) &&
!string.IsNullOrWhiteSpace(uri?.Query))
{
var queryParts = uri.Query.Substring(1).ToLowerInvariant().Split('&');
foreach(var part in queryParts)
2019-05-23 03:54:17 +03:00
{
2019-06-05 07:41:12 +03:00
if(part.StartsWith("secret="))
{
_callback(part.Substring(7)?.ToUpperInvariant());
return;
}
2019-05-23 03:54:17 +03:00
}
}
}
_callback(null);
}
2019-06-12 04:31:51 +03:00
private async void Close_Clicked(object sender, System.EventArgs e)
{
if(DoOnce())
{
await Navigation.PopModalAsync();
}
}
2019-05-23 03:54:17 +03:00
}
}