bitwarden-android/src/App/Pages/Tools/ToolsAccessibilityServicePage.cs

237 lines
8.7 KiB
C#
Raw Normal View History

2017-11-28 01:27:11 +03:00
using System;
using Bit.App.Controls;
using Xamarin.Forms;
using XLabs.Ioc;
using Bit.App.Abstractions;
using Bit.App.Resources;
2017-11-28 04:45:09 +03:00
using FFImageLoading.Forms;
2017-11-28 01:27:11 +03:00
namespace Bit.App.Pages
{
2017-11-28 04:45:09 +03:00
public class ToolsAccessibilityServicePage : ExtendedContentPage
2017-11-28 01:27:11 +03:00
{
private readonly IGoogleAnalyticsService _googleAnalyticsService;
private readonly IAppInfoService _appInfoService;
private readonly IDeviceActionService _deviceActionService;
2018-02-02 05:20:35 +03:00
private DateTime? _timerStarted = null;
private TimeSpan _timerMaxLength = TimeSpan.FromMinutes(5);
2017-11-28 01:27:11 +03:00
2017-11-28 04:45:09 +03:00
public ToolsAccessibilityServicePage()
2017-11-28 01:27:11 +03:00
{
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
_appInfoService = Resolver.Resolve<IAppInfoService>();
_deviceActionService = Resolver.Resolve<IDeviceActionService>();
Init();
}
public StackLayout EnabledStackLayout { get; set; }
public StackLayout DisabledStackLayout { get; set; }
public ScrollView ScrollView { get; set; }
public void Init()
{
var enabledFs = new FormattedString();
var statusSpan = new Span { Text = string.Concat(AppResources.Status, " ") };
enabledFs.Spans.Add(statusSpan);
enabledFs.Spans.Add(new Span
{
Text = AppResources.Enabled,
ForegroundColor = Color.Green,
FontAttributes = FontAttributes.Bold,
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label))
});
var statusEnabledLabel = new Label
{
FormattedText = enabledFs,
HorizontalTextAlignment = TextAlignment.Center,
LineBreakMode = LineBreakMode.WordWrap,
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
2017-11-28 04:45:09 +03:00
TextColor = Color.Black
2017-11-28 01:27:11 +03:00
};
var disabledFs = new FormattedString();
disabledFs.Spans.Add(statusSpan);
disabledFs.Spans.Add(new Span
{
Text = AppResources.Disabled,
ForegroundColor = Color.FromHex("c62929"),
FontAttributes = FontAttributes.Bold,
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label))
});
var statusDisabledLabel = new Label
{
FormattedText = disabledFs,
HorizontalTextAlignment = TextAlignment.Center,
LineBreakMode = LineBreakMode.WordWrap,
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
2017-11-28 04:45:09 +03:00
TextColor = Color.Black
2017-11-28 03:23:26 +03:00
};
2017-11-28 04:45:09 +03:00
var step1Label = new Label
2017-11-28 03:23:26 +03:00
{
2017-11-28 04:45:09 +03:00
Text = AppResources.BitwardenAutofillServiceStep1,
HorizontalTextAlignment = TextAlignment.Center,
LineBreakMode = LineBreakMode.WordWrap,
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
TextColor = Color.Black
};
var step1Image = new CachedImage
{
Source = "accessibility_step1",
HorizontalOptions = LayoutOptions.Center,
Margin = new Thickness(0, 20, 0, 0),
WidthRequest = 300,
HeightRequest = 98
};
var step2Label = new Label
{
Text = AppResources.BitwardenAutofillServiceStep2,
HorizontalTextAlignment = TextAlignment.Center,
LineBreakMode = LineBreakMode.WordWrap,
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
TextColor = Color.Black
};
var step2Image = new CachedImage
{
Source = "accessibility_step2",
HorizontalOptions = LayoutOptions.Center,
Margin = new Thickness(0, 20, 0, 0),
WidthRequest = 300,
HeightRequest = 67
};
var stepsStackLayout = new StackLayout
{
Children = { statusDisabledLabel, step1Image, step1Label, step2Image, step2Label },
Orientation = StackOrientation.Vertical,
Spacing = 10,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.Center
};
var notificationsLabel = new Label
{
Text = AppResources.BitwardenAutofillServiceNotification,
HorizontalTextAlignment = TextAlignment.Center,
LineBreakMode = LineBreakMode.WordWrap,
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
TextColor = Color.Black
};
var tapNotificationImage = new CachedImage
{
Source = "accessibility_notification.png",
HorizontalOptions = LayoutOptions.Center,
Margin = new Thickness(0, 20, 0, 0),
WidthRequest = 300,
HeightRequest = 74
};
var tapNotificationIcon = new CachedImage
{
Source = "accessibility_notification_icon.png",
HorizontalOptions = LayoutOptions.Center,
Margin = new Thickness(0, 20, 0, 0),
WidthRequest = 300,
HeightRequest = 54
};
var notificationsStackLayout = new StackLayout
{
Children = { statusEnabledLabel, tapNotificationIcon, tapNotificationImage, notificationsLabel },
Orientation = StackOrientation.Vertical,
Spacing = 10,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.Center
2017-11-28 01:27:11 +03:00
};
DisabledStackLayout = new StackLayout
{
2017-11-28 04:45:09 +03:00
Children = { BuildServiceLabel(), stepsStackLayout, BuildGoButton() },
2017-11-28 01:27:11 +03:00
Orientation = StackOrientation.Vertical,
Spacing = 20,
Padding = new Thickness(20, 30),
VerticalOptions = LayoutOptions.FillAndExpand
};
EnabledStackLayout = new StackLayout
{
2017-11-28 04:45:09 +03:00
Children = { BuildServiceLabel(), notificationsStackLayout, BuildGoButton() },
2017-11-28 01:27:11 +03:00
Orientation = StackOrientation.Vertical,
Spacing = 20,
Padding = new Thickness(20, 30),
VerticalOptions = LayoutOptions.FillAndExpand
};
ScrollView = new ScrollView { Content = DisabledStackLayout };
2017-12-05 21:58:09 +03:00
Title = AppResources.AutofillAccessibilityService;
Content = ScrollView;
}
2017-11-28 01:27:11 +03:00
2017-12-05 21:58:09 +03:00
protected override void OnAppearing()
{
2017-11-28 01:27:11 +03:00
UpdateEnabled();
2018-02-02 05:20:35 +03:00
_timerStarted = DateTime.Now;
2017-11-28 01:27:11 +03:00
Device.StartTimer(new TimeSpan(0, 0, 3), () =>
{
2017-12-05 21:58:09 +03:00
System.Diagnostics.Debug.WriteLine("Check timer on accessibility");
2018-02-02 05:20:35 +03:00
if(_timerStarted == null || (DateTime.Now - _timerStarted) > _timerMaxLength)
2017-11-28 01:27:11 +03:00
{
return false;
}
2017-12-05 21:58:09 +03:00
2017-11-28 01:27:11 +03:00
UpdateEnabled();
return true;
});
base.OnAppearing();
}
protected override void OnDisappearing()
{
2018-02-02 05:20:35 +03:00
_timerStarted = null;
2017-11-28 01:27:11 +03:00
base.OnDisappearing();
}
private void UpdateEnabled()
{
2017-12-05 21:58:09 +03:00
ScrollView.Content = _appInfoService.AutofillAccessibilityServiceEnabled ?
EnabledStackLayout : DisabledStackLayout;
2017-11-28 01:27:11 +03:00
}
private Label BuildServiceLabel()
{
return new Label
{
2017-11-28 04:45:09 +03:00
Text = AppResources.AutofillAccessibilityDescription,
2017-11-28 01:27:11 +03:00
VerticalOptions = LayoutOptions.Start,
HorizontalTextAlignment = TextAlignment.Center,
LineBreakMode = LineBreakMode.WordWrap,
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label))
};
}
2017-11-28 04:45:09 +03:00
private ExtendedButton BuildGoButton()
2017-11-28 01:27:11 +03:00
{
return new ExtendedButton
{
2017-11-28 04:45:09 +03:00
Text = AppResources.BitwardenAutofillServiceOpenAccessibilitySettings,
Command = new Command(() =>
2017-11-28 01:27:11 +03:00
{
2017-11-28 04:45:09 +03:00
_googleAnalyticsService.TrackAppEvent("OpenAccessibilitySettings");
_deviceActionService.OpenAccessibilitySettings();
2017-11-28 01:27:11 +03:00
}),
VerticalOptions = LayoutOptions.End,
HorizontalOptions = LayoutOptions.Fill,
2017-11-28 04:45:09 +03:00
Style = (Style)Application.Current.Resources["btn-primary"]
2017-11-28 01:27:11 +03:00
};
}
}
}