From e4d9dfc1283433a025598354d4aefb5d5b27cc64 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 28 Jul 2017 17:21:39 -0400 Subject: [PATCH] added broadcast receiver for when android updated --- src/Android/Android.csproj | 1 + src/Android/PackageReplacedReceiver.cs | 22 +++++++++++++++++++++ src/Android/Services/DeviceActionService.cs | 2 +- src/App/App.cs | 11 ++--------- src/App/Constants.cs | 1 + src/App/Utilities/Helpers.cs | 18 ++++++++++++++++- 6 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 src/Android/PackageReplacedReceiver.cs diff --git a/src/Android/Android.csproj b/src/Android/Android.csproj index 8a02b77a7..b993852c2 100644 --- a/src/Android/Android.csproj +++ b/src/Android/Android.csproj @@ -348,6 +348,7 @@ + diff --git a/src/Android/PackageReplacedReceiver.cs b/src/Android/PackageReplacedReceiver.cs new file mode 100644 index 000000000..9620c3124 --- /dev/null +++ b/src/Android/PackageReplacedReceiver.cs @@ -0,0 +1,22 @@ +using Android.App; +using Android.Content; +using Bit.App.Abstractions; +using Bit.App.Utilities; +using Plugin.Settings.Abstractions; +using System.Diagnostics; +using XLabs.Ioc; + +namespace Bit.Android +{ + [BroadcastReceiver(Name = "com.x8bit.bitwarden.PackageReplacedReceiver", Exported = true)] + [IntentFilter(new[] { Intent.ActionMyPackageReplaced })] + public class PackageReplacedReceiver : BroadcastReceiver + { + public override void OnReceive(Context context, Intent intent) + { + Debug.WriteLine("App updated!"); + Helpers.PerformUpdateTasks(Resolver.Resolve(), Resolver.Resolve(), + Resolver.Resolve()); + } + } +} diff --git a/src/Android/Services/DeviceActionService.cs b/src/Android/Services/DeviceActionService.cs index 5e3501dcb..bc2232e92 100644 --- a/src/Android/Services/DeviceActionService.cs +++ b/src/Android/Services/DeviceActionService.cs @@ -95,7 +95,7 @@ namespace Bit.Android.Services var pm = CrossCurrentActivity.Current.Activity.PackageManager; var intent = new Intent(Intent.ActionView); intent.SetType(mimeType); - var activities = pm.QueryIntentActivities(intent, global::Android.Content.PM.PackageInfoFlags.MatchDefaultOnly); + var activities = pm.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly); return (activities?.Count ?? 0) > 0; } diff --git a/src/App/App.cs b/src/App/App.cs index 1adfc1fbb..93c3dd651 100644 --- a/src/App/App.cs +++ b/src/App/App.cs @@ -13,13 +13,12 @@ using Acr.UserDialogs; using XLabs.Ioc; using System.Reflection; using Bit.App.Resources; +using Bit.App.Utilities; namespace Bit.App { public class App : Application { - private const string LastBuildKey = "LastBuild"; - private string _uri; private readonly IDatabaseService _databaseService; private readonly IConnectivity _connectivity; @@ -103,13 +102,7 @@ namespace Bit.App if(string.IsNullOrWhiteSpace(_uri)) { - var lastBuild = _settings.GetValueOrDefault(LastBuildKey); - if(Utilities.Helpers.InDebugMode() || lastBuild == null || lastBuild != _appInfoService.Build) - { - _settings.AddOrUpdateValue(LastBuildKey, _appInfoService.Build); - _databaseService.CreateTables(); - } - + Helpers.PerformUpdateTasks(_settings, _appInfoService, _databaseService); await Task.Run(() => FullSyncAsync()).ConfigureAwait(false); } diff --git a/src/App/Constants.cs b/src/App/Constants.cs index 293c22a6e..56021bfd9 100644 --- a/src/App/Constants.cs +++ b/src/App/Constants.cs @@ -33,6 +33,7 @@ public const string Locked = "other:locked"; public const string LastLoginEmail = "other:lastLoginEmail"; public const string LastSync = "other:lastSync"; + public const string LastBuildKey = "LastBuild"; public const int SelectFileRequestCode = 42; public const int SelectFilePermissionRequestCode = 43; diff --git a/src/App/Utilities/Helpers.cs b/src/App/Utilities/Helpers.cs index 3db1b2c70..4246f8bee 100644 --- a/src/App/Utilities/Helpers.cs +++ b/src/App/Utilities/Helpers.cs @@ -1,4 +1,6 @@ -using System; +using Bit.App.Abstractions; +using Plugin.Settings.Abstractions; +using System; using Xamarin.Forms; namespace Bit.App.Utilities @@ -38,5 +40,19 @@ namespace Bit.App.Utilities return false; #endif } + + public static bool PerformUpdateTasks(ISettings settings, IAppInfoService appInfoService, + IDatabaseService databaseService) + { + var lastBuild = settings.GetValueOrDefault(Constants.LastBuildKey); + if(InDebugMode() || lastBuild == null || lastBuild != appInfoService.Build) + { + settings.AddOrUpdateValue(Constants.LastBuildKey, appInfoService.Build); + databaseService.CreateTables(); + return true; + } + + return false; + } } }