bitwarden-android/src/App/Pages/Settings/SettingsPage.cs

533 lines
18 KiB
C#
Raw Normal View History

2016-05-03 00:50:16 +03:00
using System;
using Bit.App.Abstractions;
using Bit.App.Resources;
2016-05-03 00:50:16 +03:00
using Xamarin.Forms;
using XLabs.Ioc;
2016-05-13 04:30:02 +03:00
using Bit.App.Controls;
using Acr.UserDialogs;
using Plugin.Settings.Abstractions;
using Plugin.Fingerprint.Abstractions;
2016-06-30 07:36:44 +03:00
using PushNotification.Plugin.Abstractions;
2016-05-03 00:50:16 +03:00
namespace Bit.App.Pages
{
public class SettingsPage : ExtendedContentPage
2016-05-03 00:50:16 +03:00
{
private readonly IAuthService _authService;
private readonly IUserDialogs _userDialogs;
private readonly ISettings _settings;
private readonly IFingerprint _fingerprint;
2016-06-30 07:36:44 +03:00
private readonly IPushNotification _pushNotification;
private readonly IGoogleAnalyticsService _googleAnalyticsService;
// TODO: Model binding context?
2016-05-03 00:50:16 +03:00
public SettingsPage()
{
_authService = Resolver.Resolve<IAuthService>();
_userDialogs = Resolver.Resolve<IUserDialogs>();
_settings = Resolver.Resolve<ISettings>();
_fingerprint = Resolver.Resolve<IFingerprint>();
2016-06-30 07:36:44 +03:00
_pushNotification = Resolver.Resolve<IPushNotification>();
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
Init();
}
private ExtendedSwitchCell PinCell { get; set; }
private ExtendedSwitchCell FingerprintCell { get; set; }
private ExtendedTextCell LockOptionsCell { get; set; }
2017-02-18 05:18:59 +03:00
private ExtendedTextCell TwoStepCell { get; set; }
private ExtendedTextCell ChangeMasterPasswordCell { get; set; }
private ExtendedTextCell ChangeEmailCell { get; set; }
private ExtendedTextCell FoldersCell { get; set; }
private ExtendedTextCell SyncCell { get; set; }
private ExtendedTextCell LockCell { get; set; }
private ExtendedTextCell LogOutCell { get; set; }
private ExtendedTextCell AboutCell { get; set; }
private ExtendedTextCell HelpCell { get; set; }
private ExtendedTextCell RateCell { get; set; }
private ExtendedTextCell FeaturesCell { get; set; }
2017-02-18 05:18:59 +03:00
private LongDetailViewCell RateCellLong { get; set; }
2016-07-29 07:13:35 +03:00
private ExtendedTableView Table { get; set; }
private void Init()
{
PinCell = new ExtendedSwitchCell
{
Text = AppResources.UnlockWithPIN,
On = _settings.GetValueOrDefault(Constants.SettingPinUnlockOn, false)
};
LockOptionsCell = new ExtendedTextCell
{
Text = AppResources.LockOptions,
Detail = GetLockOptionsDetailsText(),
ShowDisclousure = true
};
2017-02-18 05:18:59 +03:00
TwoStepCell = new ExtendedTextCell
2016-07-28 07:39:35 +03:00
{
Text = AppResources.TwoStepLogin,
2016-07-28 07:39:35 +03:00
ShowDisclousure = true
};
var securitySecion = new TableSection(AppResources.Security)
2016-08-23 05:59:42 +03:00
{
LockOptionsCell,
PinCell,
2017-02-18 05:18:59 +03:00
TwoStepCell
2016-08-23 05:59:42 +03:00
};
if(_fingerprint.IsAvailable)
{
var fingerprintName = Device.OnPlatform(iOS: AppResources.TouchID, Android: AppResources.Fingerprint,
WinPhone: AppResources.Fingerprint);
2016-08-23 05:59:42 +03:00
FingerprintCell = new ExtendedSwitchCell
{
Text = string.Format(AppResources.UnlockWith, fingerprintName),
On = _settings.GetValueOrDefault(Constants.SettingFingerprintUnlockOn, false),
2016-08-23 05:59:42 +03:00
IsEnabled = _fingerprint.IsAvailable
};
securitySecion.Insert(1, FingerprintCell);
}
2017-02-18 05:18:59 +03:00
ChangeMasterPasswordCell = new ExtendedTextCell
{
Text = AppResources.ChangeMasterPassword,
ShowDisclousure = true
};
2017-02-18 05:18:59 +03:00
ChangeEmailCell = new ExtendedTextCell
{
Text = AppResources.ChangeEmail,
ShowDisclousure = true
};
2017-02-18 05:18:59 +03:00
FoldersCell = new ExtendedTextCell
{
Text = AppResources.Folders,
ShowDisclousure = true
};
2017-02-18 05:18:59 +03:00
SyncCell = new ExtendedTextCell
{
Text = AppResources.Sync,
ShowDisclousure = true
};
2017-02-18 05:18:59 +03:00
LockCell = new ExtendedTextCell
{
Text = AppResources.Lock
};
2017-02-18 05:18:59 +03:00
LogOutCell = new ExtendedTextCell
{
Text = AppResources.LogOut
};
2017-02-18 05:18:59 +03:00
AboutCell = new ExtendedTextCell
{
Text = AppResources.About,
ShowDisclousure = true
};
2017-02-18 05:18:59 +03:00
HelpCell = new ExtendedTextCell
{
Text = AppResources.HelpAndFeedback,
ShowDisclousure = true
};
FeaturesCell = new ExtendedTextCell
{
Text = AppResources.Features,
ShowDisclousure = true
};
var otherSection = new TableSection(AppResources.Other)
2016-08-23 05:59:42 +03:00
{
FeaturesCell,
2017-02-18 05:18:59 +03:00
AboutCell,
HelpCell
2016-08-23 05:59:42 +03:00
};
if(Device.OS == TargetPlatform.iOS)
{
2017-02-18 05:18:59 +03:00
RateCellLong = new LongDetailViewCell(AppResources.RateTheApp, AppResources.RateTheAppDescriptionAppStore);
otherSection.Add(RateCellLong);
}
else
{
2017-02-18 05:18:59 +03:00
RateCell = new ExtendedTextCell
2016-08-23 05:59:42 +03:00
{
Text = AppResources.RateTheApp,
Detail = AppResources.RateTheAppDescription,
2016-08-23 05:59:42 +03:00
ShowDisclousure = true,
DetailLineBreakMode = LineBreakMode.WordWrap
};
2017-02-18 05:18:59 +03:00
otherSection.Add(RateCell);
}
2016-07-29 07:13:35 +03:00
Table = new CustomTable
{
Root = new TableRoot
{
2016-08-23 05:59:42 +03:00
securitySecion,
new TableSection(AppResources.Account)
{
2017-02-18 05:18:59 +03:00
ChangeMasterPasswordCell,
ChangeEmailCell
2016-07-29 07:13:35 +03:00
},
new TableSection(AppResources.Manage)
{
2017-02-18 05:18:59 +03:00
FoldersCell,
SyncCell
2016-07-29 07:13:35 +03:00
},
new TableSection(AppResources.CurrentSession)
{
2017-02-18 05:18:59 +03:00
LockCell,
LogOutCell
2016-07-29 07:13:35 +03:00
},
2016-08-23 05:59:42 +03:00
otherSection
}
};
2016-05-03 00:50:16 +03:00
Title = AppResources.Settings;
Content = Table;
2016-05-03 00:50:16 +03:00
}
2017-02-18 05:18:59 +03:00
protected override void OnAppearing()
{
base.OnAppearing();
PinCell.OnChanged += PinCell_Changed;
LockOptionsCell.Tapped += LockOptionsCell_Tapped;
TwoStepCell.Tapped += TwoStepCell_Tapped;
ChangeMasterPasswordCell.Tapped += ChangeMasterPasswordCell_Tapped;
if(FingerprintCell != null)
{
FingerprintCell.OnChanged += FingerprintCell_Changed;
}
ChangeEmailCell.Tapped += ChangeEmailCell_Tapped;
FoldersCell.Tapped += FoldersCell_Tapped;
SyncCell.Tapped += SyncCell_Tapped;
LockCell.Tapped += LockCell_Tapped;
LogOutCell.Tapped += LogOutCell_Tapped;
AboutCell.Tapped += AboutCell_Tapped;
2017-02-18 05:18:59 +03:00
HelpCell.Tapped += HelpCell_Tapped;
FeaturesCell.Tapped += FeaturesCell_Tapped;
2017-02-18 05:18:59 +03:00
if(RateCellLong != null)
{
RateCellLong.Tapped += RateCell_Tapped;
}
if(RateCell != null)
{
RateCell.Tapped += RateCell_Tapped;
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
PinCell.OnChanged -= PinCell_Changed;
LockOptionsCell.Tapped -= LockOptionsCell_Tapped;
TwoStepCell.Tapped -= TwoStepCell_Tapped;
ChangeMasterPasswordCell.Tapped -= ChangeMasterPasswordCell_Tapped;
if(FingerprintCell != null)
{
FingerprintCell.OnChanged -= FingerprintCell_Changed;
}
ChangeEmailCell.Tapped -= ChangeEmailCell_Tapped;
FoldersCell.Tapped -= FoldersCell_Tapped;
SyncCell.Tapped -= SyncCell_Tapped;
LockCell.Tapped -= LockCell_Tapped;
LogOutCell.Tapped -= LogOutCell_Tapped;
AboutCell.Tapped -= AboutCell_Tapped;
HelpCell.Tapped -= HelpCell_Tapped;
FeaturesCell.Tapped -= FeaturesCell_Tapped;
2017-02-18 05:18:59 +03:00
if(RateCellLong != null)
{
RateCellLong.Tapped -= RateCell_Tapped;
}
if(RateCell != null)
{
RateCell.Tapped -= RateCell_Tapped;
}
}
2016-07-28 07:39:35 +03:00
private async void TwoStepCell_Tapped(object sender, EventArgs e)
{
if(!await _userDialogs.ConfirmAsync(AppResources.TwoStepLoginConfirmation, null, AppResources.Yes,
AppResources.Cancel))
2016-07-28 07:39:35 +03:00
{
return;
}
_googleAnalyticsService.TrackAppEvent("OpenedSetting", "TwoStep");
2016-07-28 07:39:35 +03:00
Device.OpenUri(new Uri("https://vault.bitwarden.com"));
}
private async void LockOptionsCell_Tapped(object sender, EventArgs e)
{
var selection = await DisplayActionSheet(AppResources.LockOptions, AppResources.Cancel, null,
AppResources.LockOptionImmediately, AppResources.LockOption1Minute, AppResources.LockOption15Minutes,
AppResources.LockOption1Hour, AppResources.LockOption4Hours, AppResources.Never);
if(selection == AppResources.Cancel)
{
return;
}
if(selection == AppResources.LockOptionImmediately)
{
_settings.AddOrUpdateValue(Constants.SettingLockSeconds, 0);
}
else if(selection == AppResources.LockOption1Minute)
{
_settings.AddOrUpdateValue(Constants.SettingLockSeconds, 60);
}
else if(selection == AppResources.LockOption15Minutes)
{
_settings.AddOrUpdateValue(Constants.SettingLockSeconds, 60 * 15);
}
else if(selection == AppResources.LockOption1Hour)
{
_settings.AddOrUpdateValue(Constants.SettingLockSeconds, 60 * 60);
}
else if(selection == AppResources.LockOption4Hours)
{
_settings.AddOrUpdateValue(Constants.SettingLockSeconds, 60 * 60 * 4);
}
else if(selection == AppResources.Never)
{
_settings.AddOrUpdateValue(Constants.SettingLockSeconds, -1);
}
LockOptionsCell.Detail = selection;
}
private void SyncCell_Tapped(object sender, EventArgs e)
{
Navigation.PushModalAsync(new ExtendedNavigationPage(new SettingsSyncPage()));
}
private void AboutCell_Tapped(object sender, EventArgs e)
{
Navigation.PushModalAsync(new ExtendedNavigationPage(new SettingsAboutPage()));
}
private void RateCell_Tapped(object sender, EventArgs e)
{
_googleAnalyticsService.TrackAppEvent("OpenedSetting", "RateApp");
2016-07-26 04:57:53 +03:00
if(Device.OS == TargetPlatform.iOS)
{
Device.OpenUri(new Uri($"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews" +
"?id=1137397744&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&type=Purple+Software"));
}
else if(Device.OS == TargetPlatform.Android)
{
MessagingCenter.Send(Application.Current, "RateApp");
2016-07-26 04:57:53 +03:00
}
}
private void HelpCell_Tapped(object sender, EventArgs e)
{
Navigation.PushModalAsync(new ExtendedNavigationPage(new SettingsHelpPage()));
}
private void LockCell_Tapped(object sender, EventArgs e)
{
_googleAnalyticsService.TrackAppEvent("Locked");
2016-05-22 06:26:35 +03:00
MessagingCenter.Send(Application.Current, "Lock", true);
}
private async void LogOutCell_Tapped(object sender, EventArgs e)
{
if(!await _userDialogs.ConfirmAsync(AppResources.LogoutConfirmation, null, AppResources.Yes, AppResources.Cancel))
{
return;
}
MessagingCenter.Send(Application.Current, "Logout", (string)null);
}
private async void ChangeMasterPasswordCell_Tapped(object sender, EventArgs e)
{
if(!await _userDialogs.ConfirmAsync(AppResources.ChangePasswordConfirmation, null, AppResources.Yes,
AppResources.Cancel))
{
return;
}
_googleAnalyticsService.TrackAppEvent("OpenedSetting", "ChangePassword");
Device.OpenUri(new Uri("https://vault.bitwarden.com"));
}
private async void ChangeEmailCell_Tapped(object sender, EventArgs e)
{
if(!await _userDialogs.ConfirmAsync(AppResources.ChangeEmailConfirmation, null, AppResources.Yes,
AppResources.Cancel))
{
return;
}
_googleAnalyticsService.TrackAppEvent("OpenedSetting", "ChangeEmail");
Device.OpenUri(new Uri("https://vault.bitwarden.com"));
}
private void FingerprintCell_Changed(object sender, EventArgs e)
{
var cell = sender as ExtendedSwitchCell;
if(cell == null)
{
return;
}
_settings.AddOrUpdateValue(Constants.SettingFingerprintUnlockOn, cell.On);
if(cell.On)
{
_settings.AddOrUpdateValue(Constants.SettingPinUnlockOn, false);
PinCell.On = false;
}
}
private void PinCell_Changed(object sender, EventArgs e)
{
var cell = sender as ExtendedSwitchCell;
if(cell == null)
{
return;
}
if(cell.On && !_settings.GetValueOrDefault(Constants.SettingPinUnlockOn, false))
{
cell.On = false;
2017-02-18 05:18:59 +03:00
var pinPage = new SettingsPinPage((page) => PinEntered(page));
Navigation.PushModalAsync(new ExtendedNavigationPage(pinPage));
}
else if(!cell.On)
2016-06-12 07:49:35 +03:00
{
_settings.AddOrUpdateValue(Constants.SettingPinUnlockOn, false);
}
}
2017-02-18 05:18:59 +03:00
private void PinEntered(SettingsPinPage page)
2016-06-12 07:49:35 +03:00
{
page.PinControl.Entry.Unfocus();
page.Navigation.PopModalAsync();
2016-06-12 07:49:35 +03:00
_authService.PIN = page.Model.PIN;
_settings.AddOrUpdateValue(Constants.SettingPinUnlockOn, true);
_settings.AddOrUpdateValue(Constants.SettingFingerprintUnlockOn, false);
PinCell.On = true;
2016-08-23 05:59:42 +03:00
if(FingerprintCell != null)
{
FingerprintCell.On = false;
}
}
private void FeaturesCell_Tapped(object sender, EventArgs e)
{
Navigation.PushModalAsync(new ExtendedNavigationPage(new SettingsFeaturesPage()));
}
private void FoldersCell_Tapped(object sender, EventArgs e)
{
Navigation.PushModalAsync(new ExtendedNavigationPage(new SettingsListFoldersPage()));
}
private string GetLockOptionsDetailsText()
{
var lockSeconds = _settings.GetValueOrDefault(Constants.SettingLockSeconds, 60 * 15);
if(lockSeconds == -1)
{
return AppResources.Never;
}
else if(lockSeconds == 60)
{
return AppResources.LockOption1Minute;
}
else if(lockSeconds == 60 * 15)
{
return AppResources.LockOption15Minutes;
}
else if(lockSeconds == 60 * 60)
{
return AppResources.LockOption1Hour;
}
else if(lockSeconds == 60 * 60 * 4)
{
return AppResources.LockOption4Hours;
}
else
{
return AppResources.LockOptionImmediately;
}
}
private class CustomTable : ExtendedTableView
{
public CustomTable()
{
2016-07-29 07:13:35 +03:00
Intent = TableIntent.Settings;
HasUnevenRows = true;
2016-07-28 07:39:35 +03:00
if(Device.OS == TargetPlatform.iOS)
{
RowHeight = -1;
EstimatedRowHeight = 44;
}
}
}
private class LongDetailViewCell : ExtendedViewCell
{
public LongDetailViewCell(string labelText, string detailText)
{
Label = new Label
{
2016-08-23 01:59:15 +03:00
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
VerticalOptions = LayoutOptions.CenterAndExpand,
LineBreakMode = LineBreakMode.TailTruncation,
Text = labelText
};
Detail = new Label
{
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
LineBreakMode = LineBreakMode.WordWrap,
VerticalOptions = LayoutOptions.End,
Style = (Style)Application.Current.Resources["text-muted"],
Text = detailText
};
var labelDetailStackLayout = new StackLayout
{
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
Children = { Label, Detail },
Padding = new Thickness(15)
};
ShowDisclousure = true;
View = labelDetailStackLayout;
}
public Label Label { get; set; }
public Label Detail { get; set; }
}
2016-05-03 00:50:16 +03:00
}
}