mirror of
https://github.com/bitwarden/android.git
synced 2024-12-21 00:31:58 +03:00
Proper conditions for push reregistration. Added fix for app compat crash on resume from sleep while app was open.
This commit is contained in:
parent
395342be37
commit
5c6ff45cc9
5 changed files with 83 additions and 17 deletions
|
@ -1,5 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
using Android.App;
|
using Android.App;
|
||||||
using Android.Content.PM;
|
using Android.Content.PM;
|
||||||
using Android.Views;
|
using Android.Views;
|
||||||
|
@ -10,28 +9,33 @@ using Plugin.Fingerprint.Abstractions;
|
||||||
using Plugin.Settings.Abstractions;
|
using Plugin.Settings.Abstractions;
|
||||||
using Plugin.Connectivity.Abstractions;
|
using Plugin.Connectivity.Abstractions;
|
||||||
using Acr.UserDialogs;
|
using Acr.UserDialogs;
|
||||||
using PushNotification.Plugin.Abstractions;
|
|
||||||
using Android.Content;
|
using Android.Content;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Xamarin.Forms.Platform.Android;
|
using Xamarin.Forms.Platform.Android;
|
||||||
using Xamarin.Forms;
|
using Xamarin.Forms;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Bit.Android
|
namespace Bit.Android
|
||||||
{
|
{
|
||||||
[Activity(Label = "bitwarden",
|
[Activity(Label = "bitwarden",
|
||||||
Icon = "@drawable/icon",
|
Icon = "@drawable/icon",
|
||||||
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
|
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
|
||||||
WindowSoftInputMode = SoftInput.StateHidden)]
|
WindowSoftInputMode = SoftInput.StateHidden)]
|
||||||
public class MainActivity : FormsAppCompatActivity
|
public class MainActivity : FormsAppCompatActivity
|
||||||
{
|
{
|
||||||
private const string HockeyAppId = "d3834185b4a643479047b86c65293d42";
|
private const string HockeyAppId = "d3834185b4a643479047b86c65293d42";
|
||||||
|
|
||||||
protected override void OnCreate(Bundle bundle)
|
protected async override void OnCreate(Bundle bundle)
|
||||||
{
|
{
|
||||||
ToolbarResource = Resource.Layout.toolbar;
|
ToolbarResource = Resource.Layout.toolbar;
|
||||||
TabLayoutResource = Resource.Layout.tabs;
|
TabLayoutResource = Resource.Layout.tabs;
|
||||||
|
|
||||||
base.OnCreate(bundle);
|
base.OnCreate(bundle);
|
||||||
|
|
||||||
|
// workaround for app compat bug
|
||||||
|
// ref https://forums.xamarin.com/discussion/62414/app-resuming-results-in-crash-with-formsappcompatactivity
|
||||||
|
await Task.Delay(10);
|
||||||
|
|
||||||
Console.WriteLine("A OnCreate");
|
Console.WriteLine("A OnCreate");
|
||||||
Window.SetSoftInputMode(SoftInput.StateHidden);
|
Window.SetSoftInputMode(SoftInput.StateHidden);
|
||||||
Window.AddFlags(WindowManagerFlags.Secure);
|
Window.AddFlags(WindowManagerFlags.Secure);
|
||||||
|
@ -59,8 +63,7 @@ namespace Bit.Android
|
||||||
Resolver.Resolve<ILockService>(),
|
Resolver.Resolve<ILockService>(),
|
||||||
Resolver.Resolve<IGoogleAnalyticsService>()));
|
Resolver.Resolve<IGoogleAnalyticsService>()));
|
||||||
|
|
||||||
Xamarin.Forms.MessagingCenter.Subscribe<Xamarin.Forms.Application>(
|
MessagingCenter.Subscribe<Xamarin.Forms.Application>(Xamarin.Forms.Application.Current, "RateApp", (sender) =>
|
||||||
Xamarin.Forms.Application.Current, "RateApp", (sender) =>
|
|
||||||
{
|
{
|
||||||
RateApp();
|
RateApp();
|
||||||
});
|
});
|
||||||
|
|
|
@ -17,16 +17,21 @@ using PushNotification.Plugin;
|
||||||
using PushNotification.Plugin.Abstractions;
|
using PushNotification.Plugin.Abstractions;
|
||||||
using XLabs.Ioc;
|
using XLabs.Ioc;
|
||||||
using XLabs.Ioc.Unity;
|
using XLabs.Ioc.Unity;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Plugin.Settings.Abstractions;
|
||||||
|
|
||||||
namespace Bit.Android
|
namespace Bit.Android
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
[Application(Debuggable = true)]
|
[Application(Debuggable = true)]
|
||||||
#else
|
#else
|
||||||
[Application(Debuggable=false)]
|
[Application(Debuggable = false)]
|
||||||
#endif
|
#endif
|
||||||
public class MainApplication : Application, Application.IActivityLifecycleCallbacks
|
public class MainApplication : Application, Application.IActivityLifecycleCallbacks
|
||||||
{
|
{
|
||||||
|
private const string FirstLaunchKey = "firstLaunch";
|
||||||
|
private const string LastVersionCodeKey = "lastVersionCode";
|
||||||
|
|
||||||
public static Context AppContext;
|
public static Context AppContext;
|
||||||
|
|
||||||
public MainApplication(IntPtr handle, JniHandleOwnership transer)
|
public MainApplication(IntPtr handle, JniHandleOwnership transer)
|
||||||
|
@ -38,23 +43,76 @@ namespace Bit.Android
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnCreate()
|
public async override void OnCreate()
|
||||||
{
|
{
|
||||||
base.OnCreate();
|
base.OnCreate();
|
||||||
|
|
||||||
|
// workaround for app compat bug
|
||||||
|
// ref https://forums.xamarin.com/discussion/62414/app-resuming-results-in-crash-with-formsappcompatactivity
|
||||||
|
await Task.Delay(10);
|
||||||
|
|
||||||
RegisterActivityLifecycleCallbacks(this);
|
RegisterActivityLifecycleCallbacks(this);
|
||||||
AppContext = ApplicationContext;
|
AppContext = ApplicationContext;
|
||||||
StartPushService();
|
StartPushService();
|
||||||
|
HandlePushReregistration();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandlePushReregistration()
|
||||||
|
{
|
||||||
var pushNotification = Resolver.Resolve<IPushNotification>();
|
var pushNotification = Resolver.Resolve<IPushNotification>();
|
||||||
// Must unregister the previous instance first or else things wont work
|
var settings = Resolver.Resolve<ISettings>();
|
||||||
|
|
||||||
|
// Reregister for push token based on certain conditions
|
||||||
// ref https://github.com/rdelrosario/xamarin-plugins/issues/65
|
// ref https://github.com/rdelrosario/xamarin-plugins/issues/65
|
||||||
if(Resolver.Resolve<IAuthService>().IsAuthenticated)
|
|
||||||
|
var reregister = false;
|
||||||
|
|
||||||
|
// 1. First time starting the app after a new install
|
||||||
|
if(settings.GetValueOrDefault(FirstLaunchKey, true))
|
||||||
|
{
|
||||||
|
settings.AddOrUpdateValue(FirstLaunchKey, false);
|
||||||
|
reregister = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. App version changed (installed update)
|
||||||
|
var versionCode = Context.ApplicationContext.PackageManager.GetPackageInfo(Context.PackageName, 0).VersionCode;
|
||||||
|
if(settings.GetValueOrDefault(LastVersionCodeKey, -1) != versionCode)
|
||||||
|
{
|
||||||
|
settings.AddOrUpdateValue(LastVersionCodeKey, versionCode);
|
||||||
|
reregister = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. In debug mode
|
||||||
|
if(InDebugMode())
|
||||||
|
{
|
||||||
|
reregister = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Doesn't have a push token currently
|
||||||
|
if(string.IsNullOrWhiteSpace(pushNotification.Token))
|
||||||
|
{
|
||||||
|
reregister = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(reregister)
|
||||||
{
|
{
|
||||||
pushNotification.Unregister();
|
pushNotification.Unregister();
|
||||||
pushNotification.Register();
|
if(Resolver.Resolve<IAuthService>().IsAuthenticated)
|
||||||
|
{
|
||||||
|
pushNotification.Register();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool InDebugMode()
|
||||||
|
{
|
||||||
|
#if DEBUG
|
||||||
|
return true;
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
public override void OnTerminate()
|
public override void OnTerminate()
|
||||||
{
|
{
|
||||||
base.OnTerminate();
|
base.OnTerminate();
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
using Bit.App.Abstractions;
|
using Bit.App.Abstractions;
|
||||||
using AndrodApp = Android.App.Application;
|
using AndroidApp = Android.App.Application;
|
||||||
|
|
||||||
namespace Bit.Android.Services
|
namespace Bit.Android.Services
|
||||||
{
|
{
|
||||||
public class AppInfoService : IAppInfoService
|
public class AppInfoService : IAppInfoService
|
||||||
{
|
{
|
||||||
public string Version => AndrodApp.Context.ApplicationContext.PackageManager
|
public string Version => AndroidApp.Context.ApplicationContext.PackageManager
|
||||||
.GetPackageInfo(AndrodApp.Context.PackageName, 0).VersionName;
|
.GetPackageInfo(AndroidApp.Context.PackageName, 0).VersionName;
|
||||||
|
|
||||||
public string Build => AndrodApp.Context.ApplicationContext.PackageManager
|
public string Build => AndroidApp.Context.ApplicationContext.PackageManager
|
||||||
.GetPackageInfo(AndrodApp.Context.PackageName, 0).VersionCode.ToString();
|
.GetPackageInfo(AndroidApp.Context.PackageName, 0).VersionCode.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -102,6 +102,12 @@ namespace Bit.App
|
||||||
|
|
||||||
protected async override void OnResume()
|
protected async override void OnResume()
|
||||||
{
|
{
|
||||||
|
base.OnResume();
|
||||||
|
|
||||||
|
// workaround for app compat bug
|
||||||
|
// ref https://forums.xamarin.com/discussion/62414/app-resuming-results-in-crash-with-formsappcompatactivity
|
||||||
|
await Task.Delay(10);
|
||||||
|
|
||||||
// Handle when your app resumes
|
// Handle when your app resumes
|
||||||
Debug.WriteLine("OnResume");
|
Debug.WriteLine("OnResume");
|
||||||
|
|
||||||
|
|
|
@ -205,7 +205,6 @@ namespace Bit.App.Pages
|
||||||
|
|
||||||
if(Device.OS == TargetPlatform.Android)
|
if(Device.OS == TargetPlatform.Android)
|
||||||
{
|
{
|
||||||
_pushNotification.Unregister();
|
|
||||||
_pushNotification.Register();
|
_pushNotification.Register();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue