update tasks and sync on app start

This commit is contained in:
Kyle Spearrin 2019-05-30 14:13:02 -04:00
parent dc7b37c8f2
commit 6d51864873
7 changed files with 98 additions and 4 deletions

View File

@ -1,6 +1,10 @@
using System;
using Android.App;
using Android.Content;
using Bit.App.Abstractions;
using Bit.App.Utilities;
using Bit.Core.Abstractions;
using Bit.Core.Utilities;
namespace Bit.Droid.Receivers
{
@ -8,9 +12,11 @@ namespace Bit.Droid.Receivers
[IntentFilter(new[] { Intent.ActionMyPackageReplaced })]
public class PackageReplacedReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
public override async void OnReceive(Context context, Intent intent)
{
System.Diagnostics.Debug.WriteLine("PackageReplacedReceiver OnReceive");
var storageService = ServiceContainer.Resolve<IStorageService>("storageService");
await AppHelpers.PerformUpdateTasksAsync(ServiceContainer.Resolve<ISyncService>("syncService"),
ServiceContainer.Resolve<IDeviceActionService>("deviceActionService"), storageService);
}
}
}

View File

@ -293,6 +293,12 @@ namespace Bit.Droid.Services
}
}
public string GetBuildNumber()
{
return Application.Context.ApplicationContext.PackageManager.GetPackageInfo(
Application.Context.PackageName, 0).VersionCode.ToString();
}
public bool SupportsFaceId()
{
return false;
@ -486,6 +492,26 @@ namespace Bit.Droid.Services
}
}
public bool AutofillAccessibilityServiceRunning()
{
var activity = (MainActivity)CrossCurrentActivity.Current.Activity;
var manager = activity.GetSystemService(Context.ActivityService) as ActivityManager;
var services = manager.GetRunningServices(int.MaxValue);
return services.Any(s => s.Process.ToLowerInvariant().Contains("bitwarden") &&
s.Service.ClassName.ToLowerInvariant().Contains("autofill"));
}
private bool AutofillServiceEnabled()
{
if(Build.VERSION.SdkInt < BuildVersionCodes.O)
{
return false;
}
var activity = (MainActivity)CrossCurrentActivity.Current.Activity;
var afm = (AutofillManager)activity.GetSystemService(Java.Lang.Class.FromType(typeof(AutofillManager)));
return afm.IsEnabled && afm.HasEnabledAutofillServices;
}
private bool DeleteDir(Java.IO.File dir)
{
if(dir != null && dir.IsDirectory)

View File

@ -28,5 +28,8 @@ namespace Bit.App.Abstractions
void Autofill(CipherView cipher);
void CloseAutofill();
void Background();
bool AutofillAccessibilityServiceRunning();
bool AutofillServiceEnabled();
string GetBuildNumber();
}
}

View File

@ -106,6 +106,13 @@ namespace Bit.App
{
// TODO
}
else if(message.Command == "resumed")
{
if(Device.RuntimePlatform == Device.iOS)
{
SyncIfNeeded();
}
}
});
}
@ -114,6 +121,15 @@ namespace Bit.App
System.Diagnostics.Debug.WriteLine("XF App: OnStart");
await ClearCacheIfNeededAsync();
Prime();
if(string.IsNullOrWhiteSpace(_appOptions.Uri))
{
var updated = await AppHelpers.PerformUpdateTasksAsync(_syncService, _deviceActionService,
_storageService);
if(!updated)
{
SyncIfNeeded();
}
}
}
protected async override void OnSleep()
@ -129,6 +145,10 @@ namespace Bit.App
_messagingService.Send("cancelLockTimer");
await ClearCacheIfNeededAsync();
Prime();
if(Device.RuntimePlatform == Device.Android)
{
SyncIfNeeded();
}
}
private void SetCulture()
@ -264,5 +284,17 @@ namespace Bit.App
var mainPageTask = SetMainPageAsync();
ServiceContainer.Resolve<MobilePlatformUtilsService>("platformUtilsService").Init();
}
private void SyncIfNeeded()
{
Task.Run(async () =>
{
var lastSync = await _syncService.GetLastSyncAsync();
if(DateTime.UtcNow - lastSync > TimeSpan.FromMinutes(30))
{
await _syncService.FullSyncAsync(false);
}
});
}
}
}

View File

@ -22,7 +22,8 @@ namespace Bit.App.Services
Constants.AccessibilityAutofillPersistNotificationKey,
Constants.LastActiveKey,
Constants.PushInitialPromptShownKey,
Constants.LastFileCacheClearKey
Constants.LastFileCacheClearKey,
Constants.LastBuildKey
};
public MobileStorageService(

View File

@ -1,5 +1,7 @@
using Bit.App.Pages;
using Bit.App.Abstractions;
using Bit.App.Pages;
using Bit.App.Resources;
using Bit.Core;
using Bit.Core.Abstractions;
using Bit.Core.Models.View;
using Bit.Core.Utilities;
@ -108,5 +110,28 @@ namespace Bit.App.Utilities
}
return selection;
}
public static async Task<bool> PerformUpdateTasksAsync(ISyncService syncService,
IDeviceActionService deviceActionService, IStorageService storageService)
{
var lastSync = await syncService.GetLastSyncAsync();
var currentBuild = deviceActionService.GetBuildNumber();
var lastBuild = await storageService.GetAsync<string>(Constants.LastBuildKey);
if(lastBuild == null)
{
// Installed
}
else if(lastBuild != currentBuild)
{
// Updated
var tasks = Task.Run(() => syncService.FullSyncAsync(true));
}
if(lastBuild != currentBuild)
{
await storageService.SaveAsync(Constants.LastBuildKey, currentBuild);
return true;
}
return false;
}
}
}

View File

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