From 2e7658f8572419acd3a46b1046317b92b8feaa9d Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 1 Jul 2019 16:56:42 -0400 Subject: [PATCH] clear clipboard timer --- src/iOS/AppDelegate.cs | 49 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/iOS/AppDelegate.cs b/src/iOS/AppDelegate.cs index 06856965e..e845a1830 100644 --- a/src/iOS/AppDelegate.cs +++ b/src/iOS/AppDelegate.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using AuthenticationServices; using Bit.App.Abstractions; using Bit.App.Resources; using Bit.App.Services; using Bit.App.Utilities; +using Bit.Core; using Bit.Core.Abstractions; using Bit.Core.Services; using Bit.Core.Utilities; @@ -24,6 +26,8 @@ namespace Bit.iOS private NFCNdefReaderSession _nfcSession = null; private iOSPushNotificationHandler _pushHandler = null; private NFCReaderDelegate _nfcDelegate = null; + private NSTimer _clipboardTimer = null; + private NSTimer _lockTimer = null; private IDeviceActionService _deviceActionService; private IMessagingService _messagingService; @@ -48,6 +52,7 @@ namespace Bit.iOS if(message.Command == "scheduleLockTimer") { var lockOptionMinutes = (int)message.Data; + } else if(message.Command == "cancelLockTimer") { @@ -59,7 +64,7 @@ namespace Bit.iOS } else if(message.Command == "copiedToClipboard") { - + await ClearClipboardTimerAsync(message.Data as Tuple); } else if(message.Command == "listenYubiKeyOTP") { @@ -273,6 +278,48 @@ namespace Bit.iOS } } + private async Task ClearClipboardTimerAsync(Tuple data) + { + if(data.Item3) + { + return; + } + var clearMs = data.Item2; + if(clearMs == null) + { + var clearSeconds = await _storageService.GetAsync(Constants.ClearClipboardKey); + if(clearSeconds != null) + { + clearMs = clearSeconds.Value * 1000; + } + } + if(clearMs == null) + { + return; + } + _clipboardTimer?.Invalidate(); + _clipboardTimer?.Dispose(); + _clipboardTimer = null; + var lastClipboardValue = data.Item1; + var clearMsSpan = TimeSpan.FromMilliseconds(clearMs.Value); + Device.BeginInvokeOnMainThread(() => + { + _clipboardTimer = NSTimer.CreateScheduledTimer(clearMsSpan, timer => + { + Device.BeginInvokeOnMainThread(() => + { + if(lastClipboardValue == UIPasteboard.General.String) + { + UIPasteboard.General.String = string.Empty; + } + _clipboardTimer?.Invalidate(); + _clipboardTimer?.Dispose(); + _clipboardTimer = null; + }); + }); + }); + } + private void ShowAppExtension() { var itemProvider = new NSItemProvider(new NSDictionary(), Core.Constants.UTTypeAppExtensionSetup);