1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-12-23 16:27:51 +01:00

clear clipboard alarm

This commit is contained in:
Kyle Spearrin 2019-05-30 11:22:35 -04:00
parent 49705b1074
commit 70fa41ca3e
6 changed files with 68 additions and 2 deletions

View File

@ -112,6 +112,7 @@
<Compile Include="Autofill\SavedItem.cs" />
<Compile Include="Push\FirebaseInstanceIdService.cs" />
<Compile Include="Push\FirebaseMessagingService.cs" />
<Compile Include="Receivers\ClearClipboardAlarmReceiver.cs" />
<Compile Include="Receivers\LockAlarmReceiver.cs" />
<Compile Include="Receivers\PackageReplacedReceiver.cs" />
<Compile Include="Renderers\ExtendedSliderRenderer.cs" />

View File

@ -16,6 +16,7 @@ using Bit.App.Models;
using Bit.Core.Enums;
using Android.Nfc;
using Bit.App.Utilities;
using System.Threading.Tasks;
namespace Bit.Droid
{
@ -33,7 +34,10 @@ namespace Bit.Droid
private IBroadcasterService _broadcasterService;
private IUserService _userService;
private IAppIdService _appIdService;
private IStorageService _storageService;
private IStateService _stateService;
private PendingIntent _lockAlarmPendingIntent;
private PendingIntent _clearClipboardPendingIntent;
private AppOptions _appOptions;
private const string HockeyAppId = "d3834185b4a643479047b86c65293d42";
private Java.Util.Regex.Pattern _otpPattern =
@ -44,6 +48,9 @@ namespace Bit.Droid
var alarmIntent = new Intent(this, typeof(LockAlarmReceiver));
_lockAlarmPendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent,
PendingIntentFlags.UpdateCurrent);
var clearClipboardIntent = new Intent(this, typeof(ClearClipboardAlarmReceiver));
_clearClipboardPendingIntent = PendingIntent.GetBroadcast(this, 0, clearClipboardIntent,
PendingIntentFlags.UpdateCurrent);
var policy = new StrictMode.ThreadPolicy.Builder().PermitAll().Build();
StrictMode.SetThreadPolicy(policy);
@ -53,6 +60,8 @@ namespace Bit.Droid
_broadcasterService = ServiceContainer.Resolve<IBroadcasterService>("broadcasterService");
_userService = ServiceContainer.Resolve<IUserService>("userService");
_appIdService = ServiceContainer.Resolve<IAppIdService>("appIdService");
_storageService = ServiceContainer.Resolve<IStorageService>("storageService");
_stateService = ServiceContainer.Resolve<IStateService>("stateService");
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
@ -105,6 +114,10 @@ namespace Bit.Droid
{
ExitApp();
}
else if(message.Command == "copiedToClipboard")
{
var task = ClearClipboardAlarmAsync(message.Data as Tuple<string, int?, bool>);
}
});
}
@ -293,5 +306,30 @@ namespace Bit.Droid
FinishAffinity();
Java.Lang.JavaSystem.Exit(0);
}
private async Task ClearClipboardAlarmAsync(Tuple<string, int?, bool> data)
{
if(data.Item3)
{
return;
}
var clearMs = data.Item2;
if(clearMs == null)
{
var clearSeconds = await _storageService.GetAsync<int?>(Constants.ClearClipboardKey);
if(clearSeconds != null)
{
clearMs = clearSeconds.Value * 1000;
}
}
if(clearMs == null)
{
return;
}
await _stateService.SaveAsync(Constants.LastClipboardValueKey, data.Item1);
var triggerMs = Java.Lang.JavaSystem.CurrentTimeMillis() + clearMs.Value;
var alarmManager = GetSystemService(AlarmService) as AlarmManager;
alarmManager.Set(AlarmType.Rtc, triggerMs, _clearClipboardPendingIntent);
}
}
}

View File

@ -0,0 +1,23 @@
using Android.Content;
using Bit.Core;
using Bit.Core.Abstractions;
using Bit.Core.Utilities;
namespace Bit.Droid.Receivers
{
[BroadcastReceiver(Name = "com.x8bit.bitwarden.ClearClipboardAlarmReceiver", Exported = false)]
public class ClearClipboardAlarmReceiver : BroadcastReceiver
{
public async override void OnReceive(Context context, Intent intent)
{
var stateService = ServiceContainer.Resolve<IStateService>("stateService");
var clipboardManager = context.GetSystemService(Context.ClipboardService) as ClipboardManager;
var lastClipboardValue = await stateService.GetAsync<string>(Constants.LastClipboardValueKey);
await stateService.RemoveAsync(Constants.LastClipboardValueKey);
if(lastClipboardValue == clipboardManager.Text)
{
clipboardManager.Text = string.Empty;
}
}
}
}

View File

@ -9,7 +9,6 @@ namespace Bit.Droid.Receivers
{
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

@ -179,8 +179,12 @@ namespace Bit.App.Services
public async Task CopyToClipboardAsync(string text, Dictionary<string, object> options = null)
{
var clearMs = options != null && options.ContainsKey("clearMs") ? (int?)options["clearMs"] : null;
var clearing = options != null && options.ContainsKey("clearing") ? (bool)options["clearing"] : false;
await Clipboard.SetTextAsync(text);
_messagingService.Send("copiedToClipboard", new Tuple<string, int?>(text, clearMs));
if(!clearing)
{
_messagingService.Send("copiedToClipboard", new Tuple<string, int?, bool>(text, clearMs, clearing));
}
}
public async Task<string> ReadFromClipboardAsync(Dictionary<string, object> options = null)

View File

@ -22,6 +22,7 @@
public static string PushInitialPromptShownKey = "pushInitialPromptShown";
public static string ThemeKey = "theme";
public static string ClearClipboardKey = "clearClipboard";
public static string LastClipboardValueKey = "lastClipboardValue";
public const int SelectFileRequestCode = 42;
public const int SelectFilePermissionRequestCode = 43;
}