lock alarm

This commit is contained in:
Kyle Spearrin 2019-05-16 12:30:20 -04:00
parent 84c9516659
commit 290e90ba8e
5 changed files with 86 additions and 4 deletions

View File

@ -79,6 +79,8 @@
<Compile Include="Autofill\FilledItem.cs" />
<Compile Include="Autofill\Parser.cs" />
<Compile Include="Autofill\SavedItem.cs" />
<Compile Include="Receivers\LockAlarmReceiver.cs" />
<Compile Include="Receivers\PackageReplacedReceiver.cs" />
<Compile Include="Renderers\ExtendedSliderRenderer.cs" />
<Compile Include="Renderers\CustomEditorRenderer.cs" />
<Compile Include="Renderers\CustomPickerRenderer.cs" />

View File

@ -11,6 +11,7 @@ using System.IO;
using System;
using Android.Content;
using Bit.Droid.Utilities;
using Bit.Droid.Receivers;
namespace Bit.Droid
{
@ -25,11 +26,18 @@ namespace Bit.Droid
{
private IDeviceActionService _deviceActionService;
private IMessagingService _messagingService;
private IBroadcasterService _broadcasterService;
private PendingIntent _lockAlarmPendingIntent;
protected override void OnCreate(Bundle savedInstanceState)
{
var alarmIntent = new Intent(this, typeof(LockAlarmReceiver));
_lockAlarmPendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent,
PendingIntentFlags.UpdateCurrent);
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
_messagingService = ServiceContainer.Resolve<IMessagingService>("messagingService");
_broadcasterService = ServiceContainer.Resolve<IBroadcasterService>("broadcasterService");
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
@ -38,6 +46,22 @@ namespace Bit.Droid
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App.App());
_broadcasterService.Subscribe(nameof(MainActivity), (message) =>
{
if(message.Command == "scheduleLockTimer")
{
var lockOptionMs = (int)message.Data * 1000;
var triggerMs = Java.Lang.JavaSystem.CurrentTimeMillis() + lockOptionMs + 10;
var alarmManager = GetSystemService(AlarmService) as AlarmManager;
alarmManager.Set(AlarmType.RtcWakeup, triggerMs, _lockAlarmPendingIntent);
}
else if(message.Command == "cancelLockTimer")
{
var alarmManager = GetSystemService(AlarmService) as AlarmManager;
alarmManager.Cancel(_lockAlarmPendingIntent);
}
});
}
public async override void OnRequestPermissionsResult(int requestCode, string[] permissions,

View File

@ -0,0 +1,17 @@
using Android.Content;
using Bit.Core.Abstractions;
using Bit.Core.Utilities;
namespace Bit.Droid.Receivers
{
[BroadcastReceiver(Name = "com.x8bit.bitwarden.LockAlarmReceiver", Exported = false)]
public class LockAlarmReceiver : BroadcastReceiver
{
public async override void OnReceive(Context context, Intent intent)
{
System.Diagnostics.Debug.WriteLine("LockAlarmReceiver OnReceive");
var lockService = ServiceContainer.Resolve<ILockService>("lockService");
await lockService.CheckLockAsync();
}
}
}

View File

@ -0,0 +1,16 @@
using System;
using Android.App;
using Android.Content;
namespace Bit.Droid.Receivers
{
[BroadcastReceiver(Name = "com.x8bit.bitwarden.PackageReplacedReceiver", Exported = false)]
[IntentFilter(new[] { Intent.ActionMyPackageReplaced })]
public class PackageReplacedReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
System.Diagnostics.Debug.WriteLine("PackageReplacedReceiver OnReceive");
}
}
}

View File

@ -3,6 +3,7 @@ using Bit.App.Pages;
using Bit.App.Resources;
using Bit.App.Services;
using Bit.App.Utilities;
using Bit.Core;
using Bit.Core.Abstractions;
using Bit.Core.Utilities;
using System;
@ -32,6 +33,7 @@ namespace Bit.App
private readonly ISearchService _searchService;
private readonly IPlatformUtilsService _platformUtilsService;
private readonly IAuthService _authService;
private readonly IStorageService _storageService;
public App()
{
@ -50,6 +52,7 @@ namespace Bit.App
_searchService = ServiceContainer.Resolve<ISearchService>("searchService");
_authService = ServiceContainer.Resolve<IAuthService>("authService");
_platformUtilsService = ServiceContainer.Resolve<IPlatformUtilsService>("platformUtilsService");
_storageService = ServiceContainer.Resolve<IStorageService>("storageService");
_passwordGenerationService = ServiceContainer.Resolve<IPasswordGenerationService>(
"passwordGenerationService");
_i18nService = ServiceContainer.Resolve<II18nService>("i18nService") as MobileI18nService;
@ -106,17 +109,19 @@ namespace Bit.App
protected override void OnStart()
{
// Handle when your app starts
System.Diagnostics.Debug.WriteLine("XF App: OnStart");
}
protected override void OnSleep()
protected async override void OnSleep()
{
// Handle when your app sleeps
System.Diagnostics.Debug.WriteLine("XF App: OnSleep");
await HandleLockingAsync();
}
protected override void OnResume()
{
// Handle when your app resumes
System.Diagnostics.Debug.WriteLine("XF App: OnResume");
_messagingService.Send("cancelLockTimer");
}
private void SetCulture()
@ -173,5 +178,23 @@ namespace Bit.App
Current.MainPage = new HomePage();
}
}
private async Task HandleLockingAsync()
{
var lockOption = _platformUtilsService.LockTimeout();
if(lockOption == null)
{
lockOption = await _storageService.GetAsync<int?>(Constants.LockOptionKey);
}
lockOption = lockOption.GetValueOrDefault(-1);
if(lockOption > 0)
{
_messagingService.Send("scheduleLockTimer", lockOption.Value);
}
else if(lockOption == 0)
{
// TODO: Lock now?
}
}
}
}