Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore vault timeout timer for Android #1220

Merged
merged 1 commit into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivit
private IAppIdService _appIdService;
private IStorageService _storageService;
private IEventService _eventService;
private PendingIntent _vaultTimeoutAlarmPendingIntent;
private PendingIntent _clearClipboardPendingIntent;
private PendingIntent _eventUploadPendingIntent;
private AppOptions _appOptions;
Expand All @@ -50,6 +51,9 @@ protected override void OnCreate(Bundle savedInstanceState)
var eventUploadIntent = new Intent(this, typeof(EventUploadReceiver));
_eventUploadPendingIntent = PendingIntent.GetBroadcast(this, 0, eventUploadIntent,
PendingIntentFlags.UpdateCurrent);
var alarmIntent = new Intent(this, typeof(LockAlarmReceiver));
_vaultTimeoutAlarmPendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent,
PendingIntentFlags.UpdateCurrent);
var clearClipboardIntent = new Intent(this, typeof(ClearClipboardAlarmReceiver));
_clearClipboardPendingIntent = PendingIntent.GetBroadcast(this, 0, clearClipboardIntent,
PendingIntentFlags.UpdateCurrent);
Expand Down Expand Up @@ -87,7 +91,20 @@ protected override void OnCreate(Bundle savedInstanceState)

_broadcasterService.Subscribe(_activityKey, (message) =>
{
if (message.Command == "startEventTimer")
if (message.Command == "scheduleVaultTimeoutTimer")
{
var alarmManager = GetSystemService(AlarmService) as AlarmManager;
var vaultTimeoutMinutes = (int)message.Data;
var vaultTimeoutMs = vaultTimeoutMinutes * 60000;
var triggerMs = Java.Lang.JavaSystem.CurrentTimeMillis() + vaultTimeoutMs + 10;
alarmManager.Set(AlarmType.RtcWakeup, triggerMs, _vaultTimeoutAlarmPendingIntent);
}
else if (message.Command == "cancelVaultTimeoutTimer")
{
var alarmManager = GetSystemService(AlarmService) as AlarmManager;
alarmManager.Cancel(_vaultTimeoutAlarmPendingIntent);
}
else if (message.Command == "startEventTimer")
{
StartEventAlarm();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Android/Services/DeviceActionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ public long GetActiveTime()
// be monotonic, and continues to tick even when the CPU is in power saving modes, so is the recommend
// basis for general purpose interval timing.
// ref: https://developer.android.com/reference/android/os/SystemClock#elapsedRealtime()
return SystemClock.ElapsedRealtime() / 1000;
return SystemClock.ElapsedRealtime();
}

private bool DeleteDir(Java.IO.File dir)
Expand Down
9 changes: 7 additions & 2 deletions src/App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ protected async override void OnSleep()
var isLocked = await _vaultTimeoutService.IsLockedAsync();
if (!isLocked)
{
await _storageService.SaveAsync(Constants.LastActiveKey, _deviceActionService.GetActiveTime());
await _storageService.SaveAsync(Constants.LastActiveTimeKey, _deviceActionService.GetActiveTime());
}
SetTabsPageFromAutofill(isLocked);
await SleptAsync();
Expand All @@ -211,6 +211,7 @@ private async Task SleptAsync()
private async void ResumedAsync()
{
await _vaultTimeoutService.CheckVaultTimeoutAsync();
_messagingService.Send("cancelVaultTimeoutTimer");
_messagingService.Send("startEventTimer");
await ClearCacheIfNeededAsync();
Prime();
Expand Down Expand Up @@ -302,7 +303,11 @@ private async Task HandleVaultTimeoutAsync()
vaultTimeout = await _storageService.GetAsync<int?>(Constants.VaultTimeoutKey);
}
vaultTimeout = vaultTimeout.GetValueOrDefault(-1);
if (vaultTimeout == 0)
if (vaultTimeout > 0)
{
_messagingService.Send("scheduleVaultTimeoutTimer", vaultTimeout.Value);
}
else if (vaultTimeout == 0)
{
var action = await _storageService.GetAsync<string>(Constants.VaultTimeoutActionKey);
if (action == "logOut")
Expand Down
2 changes: 1 addition & 1 deletion src/App/Pages/BaseContentPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private void SetServices()
private void SaveActivity()
{
SetServices();
_storageService.SaveAsync(Constants.LastActiveKey, _deviceActionService.GetActiveTime());
_storageService.SaveAsync(Constants.LastActiveTimeKey, _deviceActionService.GetActiveTime());
}
}
}
2 changes: 1 addition & 1 deletion src/App/Services/MobileStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class MobileStorageService : IStorageService, IDisposable
Constants.DisableFaviconKey,
Constants.ClearClipboardKey,
Constants.AutofillDisableSavePromptKey,
Constants.LastActiveKey,
Constants.LastActiveTimeKey,
Constants.PushInitialPromptShownKey,
Constants.LastFileCacheClearKey,
Constants.PushLastRegistrationDateKey,
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static class Constants
public static string SyncOnRefreshKey = "syncOnRefresh";
public static string VaultTimeoutKey = "lockOption";
public static string VaultTimeoutActionKey = "vaultTimeoutAction";
public static string LastActiveKey = "lastActive";
public static string LastActiveTimeKey = "lastActiveTime";
public static string BiometricUnlockKey = "fingerprintUnlock";
public static string ProtectedPin = "protectedPin";
public static string PinProtectedKey = "pinProtectedKey";
Expand Down
17 changes: 9 additions & 8 deletions src/Core/Services/VaultTimeoutService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,23 @@ public async Task CheckVaultTimeoutAsync()
return;
}
// This only returns null
var vaultTimeout = _platformUtilsService.LockTimeout();
if (vaultTimeout == null)
var vaultTimeoutMinutes = _platformUtilsService.LockTimeout();
if (vaultTimeoutMinutes == null)
{
vaultTimeout = await _storageService.GetAsync<int?>(Constants.VaultTimeoutKey);
vaultTimeoutMinutes = await _storageService.GetAsync<int?>(Constants.VaultTimeoutKey);
}
if (vaultTimeout.GetValueOrDefault(-1) < 0)
if (vaultTimeoutMinutes.GetValueOrDefault(-1) < 0)
{
return;
}
var lastActive = await _storageService.GetAsync<long?>(Constants.LastActiveKey);
if (lastActive == null)
var lastActiveTime = await _storageService.GetAsync<long?>(Constants.LastActiveTimeKey);
if (lastActiveTime == null)
{
return;
}
var diff = _platformUtilsService.GetActiveTime() - lastActive;
if (diff >= vaultTimeout * 60)
var diffMs = _platformUtilsService.GetActiveTime() - lastActiveTime;
var vaultTimeoutMs = vaultTimeoutMinutes * 60000;
if (diffMs >= vaultTimeoutMs)
{
// Pivot based on saved action
var action = await _storageService.GetAsync<string>(Constants.VaultTimeoutActionKey);
Expand Down
6 changes: 3 additions & 3 deletions src/iOS.Core/Services/DeviceActionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,9 @@ public bool UsingDarkTheme()

public long GetActiveTime()
{
// Fall back to UnixTimeSeconds in case this approach stops working. We'll lose clock-change protection but
// the lock functionality will continue to work.
return iOSHelpers.GetSystemUpTimeSeconds() ?? DateTimeOffset.UtcNow.ToUnixTimeSeconds();
// Fall back to UnixTimeMilliseconds in case this approach stops working. We'll lose clock-change
// protection but the lock functionality will continue to work.
return iOSHelpers.GetSystemUpTimeMilliseconds() ?? DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
}

private void ImagePicker_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions src/iOS.Core/Utilities/iOSHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static extern int sysctlbyname([MarshalAs(UnmanagedType.LPStr)] string
// includes sleep time.
// ref: https://forums.xamarin.com/discussion/20006/access-to-sysctl-h
// ref: https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/src/Platform/XLabs.Platform.iOS/Device/AppleDevice.cs
public static long? GetSystemUpTimeSeconds()
public static long? GetSystemUpTimeMilliseconds()
{
long? uptime = null;
IntPtr pLen = default, pStr = default;
Expand All @@ -34,7 +34,7 @@ internal static extern int sysctlbyname([MarshalAs(UnmanagedType.LPStr)] string
var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
if (timeVal.sec > 0 && now > 0)
{
uptime = now - timeVal.sec;
uptime = (now - timeVal.sec) * 1000;
}
}
catch (Exception e)
Expand Down
2 changes: 1 addition & 1 deletion src/iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public override void DidEnterBackground(UIApplication uiApplication)
UIApplication.SharedApplication.KeyWindow.BringSubviewToFront(view);
UIApplication.SharedApplication.KeyWindow.EndEditing(true);
UIApplication.SharedApplication.SetStatusBarHidden(true, false);
_storageService.SaveAsync(Constants.LastActiveKey, _deviceActionService.GetActiveTime());
_storageService.SaveAsync(Constants.LastActiveTimeKey, _deviceActionService.GetActiveTime());
_messagingService.Send("slept");
base.DidEnterBackground(uiApplication);
}
Expand Down