diff --git a/src/Android/Android.csproj b/src/Android/Android.csproj
index bc1ef7f46..a893480fc 100644
--- a/src/Android/Android.csproj
+++ b/src/Android/Android.csproj
@@ -79,6 +79,8 @@
+
+
diff --git a/src/Android/MainActivity.cs b/src/Android/MainActivity.cs
index fba508cb6..f70537aef 100644
--- a/src/Android/MainActivity.cs
+++ b/src/Android/MainActivity.cs
@@ -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("deviceActionService");
_messagingService = ServiceContainer.Resolve("messagingService");
+ _broadcasterService = ServiceContainer.Resolve("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,
diff --git a/src/Android/Receivers/LockAlarmReceiver.cs b/src/Android/Receivers/LockAlarmReceiver.cs
new file mode 100644
index 000000000..b4a00e3cc
--- /dev/null
+++ b/src/Android/Receivers/LockAlarmReceiver.cs
@@ -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("lockService");
+ await lockService.CheckLockAsync();
+ }
+ }
+}
diff --git a/src/Android/Receivers/PackageReplacedReceiver.cs b/src/Android/Receivers/PackageReplacedReceiver.cs
new file mode 100644
index 000000000..ef0cfba46
--- /dev/null
+++ b/src/Android/Receivers/PackageReplacedReceiver.cs
@@ -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");
+ }
+ }
+}
diff --git a/src/App/App.xaml.cs b/src/App/App.xaml.cs
index e2ec08a7e..8c778006f 100644
--- a/src/App/App.xaml.cs
+++ b/src/App/App.xaml.cs
@@ -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("searchService");
_authService = ServiceContainer.Resolve("authService");
_platformUtilsService = ServiceContainer.Resolve("platformUtilsService");
+ _storageService = ServiceContainer.Resolve("storageService");
_passwordGenerationService = ServiceContainer.Resolve(
"passwordGenerationService");
_i18nService = ServiceContainer.Resolve("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(Constants.LockOptionKey);
+ }
+ lockOption = lockOption.GetValueOrDefault(-1);
+ if(lockOption > 0)
+ {
+ _messagingService.Send("scheduleLockTimer", lockOption.Value);
+ }
+ else if(lockOption == 0)
+ {
+ // TODO: Lock now?
+ }
+ }
}
}