-
Notifications
You must be signed in to change notification settings - Fork 832
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed Clipboard clear after x seconds depending on what the user set.…
… Also refactored a bit to make the Clipboard a custom service to provide a better way to handle this situation #1464
- Loading branch information
Showing
17 changed files
with
150 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Android.App; | ||
using Android.Content; | ||
using Bit.Core; | ||
using Bit.Core.Abstractions; | ||
using Bit.Droid.Receivers; | ||
using Plugin.CurrentActivity; | ||
using Xamarin.Essentials; | ||
|
||
namespace Bit.Droid.Services | ||
{ | ||
public class ClipboardService : IClipboardService | ||
{ | ||
private readonly IStorageService _storageService; | ||
private readonly Lazy<PendingIntent> _clearClipboardPendingIntent; | ||
|
||
public ClipboardService(IStorageService storageService) | ||
{ | ||
_storageService = storageService; | ||
|
||
_clearClipboardPendingIntent = new Lazy<PendingIntent>(() => | ||
PendingIntent.GetBroadcast(CrossCurrentActivity.Current.Activity, | ||
0, | ||
new Intent(CrossCurrentActivity.Current.Activity, typeof(ClearClipboardAlarmReceiver)), | ||
PendingIntentFlags.UpdateCurrent)); | ||
} | ||
|
||
public async Task CopyTextAsync(string text, int expiresInMs = -1) | ||
{ | ||
await Clipboard.SetTextAsync(text); | ||
|
||
await ClearClipboardAlarmAsync(expiresInMs); | ||
} | ||
|
||
private async Task ClearClipboardAlarmAsync(int expiresInMs = -1) | ||
{ | ||
var clearMs = expiresInMs; | ||
if (clearMs < 0) | ||
{ | ||
// if not set then we need to check if the user set this config | ||
var clearSeconds = await _storageService.GetAsync<int?>(Constants.ClearClipboardKey); | ||
if (clearSeconds != null) | ||
{ | ||
clearMs = clearSeconds.Value * 1000; | ||
} | ||
} | ||
if (clearMs < 0) | ||
{ | ||
return; | ||
} | ||
var triggerMs = Java.Lang.JavaSystem.CurrentTimeMillis() + clearMs; | ||
var alarmManager = CrossCurrentActivity.Current.Activity.GetSystemService(Context.AlarmService) as AlarmManager; | ||
alarmManager.Set(AlarmType.Rtc, triggerMs, _clearClipboardPendingIntent.Value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.