From ab3bebf06aaa5b5a3b123eb2281fdd83e9fdeaf6 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 1 Jul 2019 23:44:47 -0400 Subject: [PATCH] use background tasks to keep timers alive --- .../Pages/Settings/OptionsPageViewModel.cs | 10 ++- src/iOS/AppDelegate.cs | 67 +++++++++++++++---- 2 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/App/Pages/Settings/OptionsPageViewModel.cs b/src/App/Pages/Settings/OptionsPageViewModel.cs index 290f1cc25..08d7b0638 100644 --- a/src/App/Pages/Settings/OptionsPageViewModel.cs +++ b/src/App/Pages/Settings/OptionsPageViewModel.cs @@ -46,6 +46,7 @@ namespace Bit.App.Pages _messagingService = ServiceContainer.Resolve("messagingService"); PageTitle = AppResources.Options; + var iosIos = Device.RuntimePlatform == Device.iOS; ClearClipboardOptions = new List> { @@ -53,10 +54,13 @@ namespace Bit.App.Pages new KeyValuePair(10, AppResources.TenSeconds), new KeyValuePair(20, AppResources.TwentySeconds), new KeyValuePair(30, AppResources.ThirtySeconds), - new KeyValuePair(60, AppResources.OneMinute), - new KeyValuePair(120, AppResources.TwoMinutes), - new KeyValuePair(300, AppResources.FiveMinutes), + new KeyValuePair(60, AppResources.OneMinute) }; + if(!iosIos) + { + ClearClipboardOptions.Add(new KeyValuePair(120, AppResources.TwoMinutes)); + ClearClipboardOptions.Add(new KeyValuePair(300, AppResources.FiveMinutes)); + } ThemeOptions = new List> { new KeyValuePair(null, AppResources.Default), diff --git a/src/iOS/AppDelegate.cs b/src/iOS/AppDelegate.cs index e9c33c5a8..03ecd6433 100644 --- a/src/iOS/AppDelegate.cs +++ b/src/iOS/AppDelegate.cs @@ -27,7 +27,9 @@ namespace Bit.iOS private iOSPushNotificationHandler _pushHandler = null; private NFCReaderDelegate _nfcDelegate = null; private NSTimer _clipboardTimer = null; + private nint _clipboardBackgroundTaskId; private NSTimer _lockTimer = null; + private nint _lockBackgroundTaskId; private IDeviceActionService _deviceActionService; private IMessagingService _messagingService; @@ -65,7 +67,11 @@ namespace Bit.iOS } else if(message.Command == "copiedToClipboard") { - await ClearClipboardTimerAsync(message.Data as Tuple); + + Device.BeginInvokeOnMainThread(() => + { + var task = ClearClipboardTimerAsync(message.Data as Tuple); + }); } else if(message.Command == "listenYubiKeyOTP") { @@ -281,11 +287,21 @@ namespace Bit.iOS private void LockTimer(int lockOptionMinutes) { + if(_lockBackgroundTaskId > 0) + { + UIApplication.SharedApplication.EndBackgroundTask(_lockBackgroundTaskId); + _lockBackgroundTaskId = 0; + } + _lockBackgroundTaskId = UIApplication.SharedApplication.BeginBackgroundTask(() => + { + UIApplication.SharedApplication.EndBackgroundTask(_lockBackgroundTaskId); + _lockBackgroundTaskId = 0; + }); var lockOptionMs = lockOptionMinutes * 60000; _lockTimer?.Invalidate(); _lockTimer?.Dispose(); _lockTimer = null; - var lockMsSpan = TimeSpan.FromMilliseconds(lockOptionMs + 10); + var lockMsSpan = TimeSpan.FromMilliseconds(10000 + 10); Device.BeginInvokeOnMainThread(() => { _lockTimer = NSTimer.CreateScheduledTimer(lockMsSpan, timer => @@ -296,6 +312,11 @@ namespace Bit.iOS _lockTimer?.Invalidate(); _lockTimer?.Dispose(); _lockTimer = null; + if(_lockBackgroundTaskId > 0) + { + UIApplication.SharedApplication.EndBackgroundTask(_lockBackgroundTaskId); + _lockBackgroundTaskId = 0; + } }); }); }); @@ -306,6 +327,11 @@ namespace Bit.iOS _lockTimer?.Invalidate(); _lockTimer?.Dispose(); _lockTimer = null; + if(_lockBackgroundTaskId > 0) + { + UIApplication.SharedApplication.EndBackgroundTask(_lockBackgroundTaskId); + _lockBackgroundTaskId = 0; + } } private async Task ClearClipboardTimerAsync(Tuple data) @@ -327,25 +353,38 @@ namespace Bit.iOS { return; } + if(_clipboardBackgroundTaskId > 0) + { + UIApplication.SharedApplication.EndBackgroundTask(_clipboardBackgroundTaskId); + _clipboardBackgroundTaskId = 0; + } + _clipboardBackgroundTaskId = UIApplication.SharedApplication.BeginBackgroundTask(() => + { + UIApplication.SharedApplication.EndBackgroundTask(_clipboardBackgroundTaskId); + _clipboardBackgroundTaskId = 0; + }); _clipboardTimer?.Invalidate(); _clipboardTimer?.Dispose(); _clipboardTimer = null; - var lastClipboardValue = data.Item1; + var lastClipboardChangeCount = UIPasteboard.General.ChangeCount; var clearMsSpan = TimeSpan.FromMilliseconds(clearMs.Value); - Device.BeginInvokeOnMainThread(() => + _clipboardTimer = NSTimer.CreateScheduledTimer(clearMsSpan, timer => { - _clipboardTimer = NSTimer.CreateScheduledTimer(clearMsSpan, timer => + Device.BeginInvokeOnMainThread(() => { - Device.BeginInvokeOnMainThread(() => + var changeNow = UIPasteboard.General.ChangeCount; + if(changeNow == 0 || lastClipboardChangeCount == changeNow) { - if(lastClipboardValue == UIPasteboard.General.String) - { - UIPasteboard.General.String = string.Empty; - } - _clipboardTimer?.Invalidate(); - _clipboardTimer?.Dispose(); - _clipboardTimer = null; - }); + UIPasteboard.General.String = string.Empty; + } + _clipboardTimer?.Invalidate(); + _clipboardTimer?.Dispose(); + _clipboardTimer = null; + if(_clipboardBackgroundTaskId > 0) + { + UIApplication.SharedApplication.EndBackgroundTask(_clipboardBackgroundTaskId); + _clipboardBackgroundTaskId = 0; + } }); }); }