bitwarden-android/src/App/Pages/Vault/VaultAddLoginPage.cs

302 lines
12 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
using System.Collections.Generic;
2016-05-02 09:52:09 +03:00
using System.Linq;
2016-05-03 09:08:50 +03:00
using Acr.UserDialogs;
2016-05-02 09:52:09 +03:00
using Bit.App.Abstractions;
2016-05-10 06:25:37 +03:00
using Bit.App.Controls;
2016-05-02 09:52:09 +03:00
using Bit.App.Models;
using Bit.App.Resources;
2016-05-07 01:49:01 +03:00
using Plugin.Connectivity.Abstractions;
2016-05-02 09:52:09 +03:00
using Xamarin.Forms;
using XLabs.Ioc;
using Plugin.Settings.Abstractions;
2017-05-30 21:13:53 +03:00
using Bit.App.Utilities;
2016-05-02 09:52:09 +03:00
2016-05-03 00:50:16 +03:00
namespace Bit.App.Pages
2016-05-02 09:52:09 +03:00
{
2017-01-03 08:17:15 +03:00
public class VaultAddLoginPage : ExtendedContentPage
2016-05-02 09:52:09 +03:00
{
2017-01-03 08:17:15 +03:00
private const string AddedLoginAlertKey = "addedSiteAlert";
2017-01-03 08:17:15 +03:00
private readonly ILoginService _loginService;
private readonly IFolderService _folderService;
private readonly IUserDialogs _userDialogs;
private readonly IConnectivity _connectivity;
private readonly IGoogleAnalyticsService _googleAnalyticsService;
private readonly ISettings _settings;
private readonly IAppInfoService _appInfoService;
2017-01-31 03:26:39 +03:00
private readonly string _defaultUri;
private readonly string _defaultName;
private readonly bool _fromAutofill;
private DateTime? _lastAction;
public VaultAddLoginPage(string defaultUri = null, string defaultName = null, bool fromAutofill = false)
2016-05-02 09:52:09 +03:00
{
2017-01-31 03:26:39 +03:00
_defaultUri = defaultUri;
_defaultName = defaultName;
_fromAutofill = fromAutofill;
2017-01-31 03:26:39 +03:00
2017-01-03 08:17:15 +03:00
_loginService = Resolver.Resolve<ILoginService>();
_folderService = Resolver.Resolve<IFolderService>();
_userDialogs = Resolver.Resolve<IUserDialogs>();
_connectivity = Resolver.Resolve<IConnectivity>();
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
_settings = Resolver.Resolve<ISettings>();
_appInfoService = Resolver.Resolve<IAppInfoService>();
2016-05-10 01:16:42 +03:00
Init();
}
public FormEntryCell PasswordCell { get; private set; }
public FormEntryCell UsernameCell { get; private set; }
public FormEntryCell UriCell { get; private set; }
public FormEntryCell NameCell { get; private set; }
2017-07-13 18:52:24 +03:00
public FormEntryCell TotpCell { get; private set; }
public FormEditorCell NotesCell { get; private set; }
public FormPickerCell FolderCell { get; private set; }
public ExtendedTextCell GenerateCell { get; private set; }
private void Init()
{
NotesCell = new FormEditorCell(height: 180);
2017-07-13 18:52:24 +03:00
TotpCell = new FormEntryCell(AppResources.AuthenticatorKey, nextElement: NotesCell.Editor,
useButton: true);
TotpCell.Button.Image = "camera";
TotpCell.Entry.DisableAutocapitalize = true;
TotpCell.Entry.Autocorrect = false;
TotpCell.Entry.FontFamily = Helpers.OnPlatform(iOS: "Courier", Android: "monospace", WinPhone: "Courier");
PasswordCell = new FormEntryCell(AppResources.Password, isPassword: true, nextElement: TotpCell.Entry,
useButton: true);
PasswordCell.Button.Image = "eye";
PasswordCell.Entry.DisableAutocapitalize = true;
PasswordCell.Entry.Autocorrect = false;
2017-05-30 21:13:53 +03:00
PasswordCell.Entry.FontFamily = Helpers.OnPlatform(iOS: "Courier", Android: "monospace", WinPhone: "Courier");
UsernameCell = new FormEntryCell(AppResources.Username, nextElement: PasswordCell.Entry);
UsernameCell.Entry.DisableAutocapitalize = true;
UsernameCell.Entry.Autocorrect = false;
UriCell = new FormEntryCell(AppResources.URI, Keyboard.Url, nextElement: UsernameCell.Entry);
2017-01-31 03:26:39 +03:00
if(!string.IsNullOrWhiteSpace(_defaultUri))
{
UriCell.Entry.Text = _defaultUri;
2017-01-31 03:26:39 +03:00
}
NameCell = new FormEntryCell(AppResources.Name, nextElement: UriCell.Entry);
2017-01-31 03:26:39 +03:00
if(!string.IsNullOrWhiteSpace(_defaultName))
{
NameCell.Entry.Text = _defaultName;
2017-01-31 03:26:39 +03:00
}
2016-05-02 09:52:09 +03:00
var folderOptions = new List<string> { AppResources.FolderNone };
var folders = _folderService.GetAllAsync().GetAwaiter().GetResult()
.OrderBy(f => f.Name?.Decrypt()).ToList();
2016-05-12 00:30:09 +03:00
foreach(var folder in folders)
{
folderOptions.Add(folder.Name.Decrypt());
}
FolderCell = new FormPickerCell(AppResources.Folder, folderOptions.ToArray());
GenerateCell = new ExtendedTextCell
{
2016-11-26 00:42:52 +03:00
Text = AppResources.GeneratePassword,
ShowDisclousure = true
};
2016-11-26 00:42:52 +03:00
var favoriteCell = new ExtendedSwitchCell { Text = AppResources.Favorite };
2016-06-15 05:36:37 +03:00
2016-05-24 06:48:34 +03:00
var table = new ExtendedTableView
2016-05-12 00:30:09 +03:00
{
Intent = TableIntent.Settings,
2016-05-24 06:48:34 +03:00
EnableScrolling = true,
2016-05-12 00:30:09 +03:00
HasUnevenRows = true,
Root = new TableRoot
{
2017-01-03 08:17:15 +03:00
new TableSection(AppResources.LoginInformation)
2016-05-12 00:30:09 +03:00
{
NameCell,
UriCell,
UsernameCell,
PasswordCell,
GenerateCell
2016-05-13 04:30:02 +03:00
},
new TableSection(" ")
2016-06-15 05:36:37 +03:00
{
2017-07-13 18:52:24 +03:00
TotpCell,
FolderCell,
2016-06-15 05:36:37 +03:00
favoriteCell
},
new TableSection(AppResources.Notes)
{
NotesCell
2016-05-12 00:30:09 +03:00
}
}
};
2016-05-02 09:52:09 +03:00
2017-05-30 21:13:53 +03:00
if(Device.RuntimePlatform == Device.iOS)
{
2016-05-24 06:48:34 +03:00
table.RowHeight = -1;
table.EstimatedRowHeight = 70;
}
2017-05-30 21:13:53 +03:00
else if(Device.RuntimePlatform == Device.Android)
{
2017-07-13 18:52:24 +03:00
PasswordCell.Button.WidthRequest = TotpCell.Button.WidthRequest = 40;
}
2016-05-12 00:30:09 +03:00
var saveToolBarItem = new ToolbarItem(AppResources.Save, null, async () =>
{
if(_lastAction.LastActionWasRecent())
{
return;
}
_lastAction = DateTime.UtcNow;
2016-05-12 00:30:09 +03:00
if(!_connectivity.IsConnected)
{
AlertNoConnection();
return;
}
if(string.IsNullOrWhiteSpace(NameCell.Entry.Text))
2016-05-12 00:30:09 +03:00
{
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
AppResources.Name), AppResources.Ok);
2016-05-12 00:30:09 +03:00
return;
}
2017-01-03 08:17:15 +03:00
var login = new Login
2016-05-12 00:30:09 +03:00
{
2017-07-13 21:44:02 +03:00
Name = NameCell.Entry.Text.Encrypt(),
Uri = string.IsNullOrWhiteSpace(UriCell.Entry.Text) ? null : UriCell.Entry.Text.Encrypt(),
Username = string.IsNullOrWhiteSpace(UsernameCell.Entry.Text) ? null : UsernameCell.Entry.Text.Encrypt(),
Password = string.IsNullOrWhiteSpace(PasswordCell.Entry.Text) ? null : PasswordCell.Entry.Text.Encrypt(),
Notes = string.IsNullOrWhiteSpace(NotesCell.Editor.Text) ? null : NotesCell.Editor.Text.Encrypt(),
Totp = string.IsNullOrWhiteSpace(TotpCell.Entry.Text) ? null : TotpCell.Entry.Text.Encrypt(),
2016-06-15 05:36:37 +03:00
Favorite = favoriteCell.On
2016-05-12 00:30:09 +03:00
};
if(FolderCell.Picker.SelectedIndex > 0)
2016-05-12 00:30:09 +03:00
{
login.FolderId = folders.ElementAt(FolderCell.Picker.SelectedIndex - 1).Id;
2016-05-12 00:30:09 +03:00
}
2016-11-26 00:42:52 +03:00
_userDialogs.ShowLoading(AppResources.Saving, MaskType.Black);
2017-01-03 08:17:15 +03:00
var saveTask = await _loginService.SaveAsync(login);
2016-05-12 00:30:09 +03:00
_userDialogs.HideLoading();
2017-06-07 17:19:56 +03:00
if(saveTask.Succeeded)
{
2017-01-03 08:17:15 +03:00
_userDialogs.Toast(AppResources.NewLoginCreated);
if(_fromAutofill)
{
_googleAnalyticsService.TrackExtensionEvent("CreatedLogin");
}
else
{
_googleAnalyticsService.TrackAppEvent("CreatedLogin");
}
2017-06-07 17:19:56 +03:00
await Navigation.PopForDeviceAsync();
}
else if(saveTask.Errors.Count() > 0)
{
await _userDialogs.AlertAsync(saveTask.Errors.First().Message, AppResources.AnErrorHasOccurred);
}
else
{
await _userDialogs.AlertAsync(AppResources.AnErrorHasOccurred);
}
2016-05-12 00:30:09 +03:00
}, ToolbarItemOrder.Default, 0);
2016-05-02 09:52:09 +03:00
2017-01-03 08:17:15 +03:00
Title = AppResources.AddLogin;
2016-05-24 06:48:34 +03:00
Content = table;
2016-05-12 00:30:09 +03:00
ToolbarItems.Add(saveToolBarItem);
2017-05-30 21:13:53 +03:00
if(Device.RuntimePlatform == Device.iOS)
{
2016-11-26 00:42:52 +03:00
ToolbarItems.Add(new DismissModalToolBarItem(this, AppResources.Cancel));
}
}
2016-05-07 01:49:01 +03:00
protected override void OnAppearing()
{
base.OnAppearing();
2016-05-12 00:30:09 +03:00
if(!_connectivity.IsConnected)
2016-05-07 01:49:01 +03:00
{
AlertNoConnection();
}
PasswordCell.InitEvents();
UsernameCell.InitEvents();
UriCell.InitEvents();
NameCell.InitEvents();
NotesCell.InitEvents();
2017-07-13 18:52:24 +03:00
TotpCell.InitEvents();
FolderCell.InitEvents();
PasswordCell.Button.Clicked += PasswordButton_Clicked;
2017-07-13 18:52:24 +03:00
TotpCell.Button.Clicked += TotpButton_Clicked;
GenerateCell.Tapped += GenerateCell_Tapped;
if(!_fromAutofill && !_settings.GetValueOrDefault(AddedLoginAlertKey, false))
{
2017-01-03 08:17:15 +03:00
_settings.AddOrUpdateValue(AddedLoginAlertKey, true);
2017-05-30 21:13:53 +03:00
if(Device.RuntimePlatform == Device.iOS)
{
DisplayAlert(AppResources.BitwardenAppExtension, AppResources.BitwardenAppExtensionAlert,
AppResources.Ok);
}
2017-05-30 21:13:53 +03:00
else if(Device.RuntimePlatform == Device.Android && !_appInfoService.AutofillServiceEnabled)
{
DisplayAlert(AppResources.BitwardenAutofillService, AppResources.BitwardenAutofillServiceAlert,
AppResources.Ok);
}
}
NameCell.Entry.FocusWithDelay();
2016-05-07 01:49:01 +03:00
}
protected override void OnDisappearing()
{
base.OnDisappearing();
PasswordCell.Dispose();
UsernameCell.Dispose();
UriCell.Dispose();
NameCell.Dispose();
NotesCell.Dispose();
2017-07-13 18:52:24 +03:00
TotpCell.Dispose();
FolderCell.Dispose();
PasswordCell.Button.Clicked -= PasswordButton_Clicked;
2017-07-13 18:52:24 +03:00
TotpCell.Button.Clicked -= TotpButton_Clicked;
GenerateCell.Tapped -= GenerateCell_Tapped;
}
private void PasswordButton_Clicked(object sender, EventArgs e)
{
PasswordCell.Entry.InvokeToggleIsPassword();
PasswordCell.Button.Image = "eye" + (!PasswordCell.Entry.IsPasswordFromToggled ? "_slash" : string.Empty);
}
2017-07-13 18:52:24 +03:00
private void TotpButton_Clicked(object sender, EventArgs e)
{
// launch camera
}
private async void GenerateCell_Tapped(object sender, EventArgs e)
{
var page = new ToolsPasswordGeneratorPage((password) =>
{
PasswordCell.Entry.Text = password;
2016-11-26 00:42:52 +03:00
_userDialogs.Toast(AppResources.PasswordGenerated);
}, _fromAutofill);
await Navigation.PushForDeviceAsync(page);
}
private void AlertNoConnection()
2016-05-07 01:49:01 +03:00
{
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage,
AppResources.Ok);
2016-05-12 00:30:09 +03:00
}
2016-05-02 09:52:09 +03:00
}
}
2016-05-07 01:49:01 +03:00