2016-06-18 09:45:46 +03:00
|
|
|
using System;
|
|
|
|
using Acr.UserDialogs;
|
|
|
|
using Android.App;
|
|
|
|
using Android.Content;
|
|
|
|
using Android.OS;
|
|
|
|
using Android.Runtime;
|
|
|
|
using Bit.Android.Services;
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
using Bit.App.Repositories;
|
|
|
|
using Bit.App.Services;
|
|
|
|
using Microsoft.Practices.Unity;
|
|
|
|
using Plugin.Connectivity;
|
|
|
|
using Plugin.CurrentActivity;
|
|
|
|
using Plugin.Fingerprint;
|
|
|
|
using Plugin.Settings;
|
|
|
|
using PushNotification.Plugin;
|
|
|
|
using PushNotification.Plugin.Abstractions;
|
|
|
|
using XLabs.Ioc;
|
|
|
|
using XLabs.Ioc.Unity;
|
2016-08-27 21:36:32 +03:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Plugin.Settings.Abstractions;
|
2016-12-23 08:39:00 +03:00
|
|
|
using Xamarin.Android.Net;
|
2016-06-18 09:45:46 +03:00
|
|
|
|
|
|
|
namespace Bit.Android
|
|
|
|
{
|
2016-08-27 06:07:35 +03:00
|
|
|
#if DEBUG
|
|
|
|
[Application(Debuggable = true)]
|
|
|
|
#else
|
2016-08-27 21:36:32 +03:00
|
|
|
[Application(Debuggable = false)]
|
2016-08-27 06:07:35 +03:00
|
|
|
#endif
|
2016-06-18 09:45:46 +03:00
|
|
|
public class MainApplication : Application, Application.IActivityLifecycleCallbacks
|
|
|
|
{
|
2016-08-27 21:36:32 +03:00
|
|
|
private const string FirstLaunchKey = "firstLaunch";
|
|
|
|
private const string LastVersionCodeKey = "lastVersionCode";
|
|
|
|
|
2016-06-18 09:45:46 +03:00
|
|
|
public static Context AppContext;
|
|
|
|
|
|
|
|
public MainApplication(IntPtr handle, JniHandleOwnership transer)
|
|
|
|
: base(handle, transer)
|
|
|
|
{
|
2016-12-23 08:39:00 +03:00
|
|
|
// NOTE: This is just here to stop the linker from removing AndroidClientHandler references
|
|
|
|
var handler = new AndroidClientHandler();
|
|
|
|
|
2016-06-18 09:45:46 +03:00
|
|
|
if(!Resolver.IsSet)
|
|
|
|
{
|
2017-01-28 07:13:28 +03:00
|
|
|
SetIoc(this);
|
2016-06-18 09:45:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-27 22:00:12 +03:00
|
|
|
public override void OnCreate()
|
2016-06-18 09:45:46 +03:00
|
|
|
{
|
|
|
|
base.OnCreate();
|
2016-08-27 21:36:32 +03:00
|
|
|
|
|
|
|
// workaround for app compat bug
|
|
|
|
// ref https://forums.xamarin.com/discussion/62414/app-resuming-results-in-crash-with-formsappcompatactivity
|
2016-08-27 22:00:12 +03:00
|
|
|
Task.Delay(10).Wait();
|
2016-08-27 21:36:32 +03:00
|
|
|
|
2016-06-18 09:45:46 +03:00
|
|
|
RegisterActivityLifecycleCallbacks(this);
|
|
|
|
AppContext = ApplicationContext;
|
2017-02-16 03:55:52 +03:00
|
|
|
//StartPushService();
|
|
|
|
//HandlePushReregistration();
|
2016-08-27 21:36:32 +03:00
|
|
|
}
|
2016-08-21 02:19:10 +03:00
|
|
|
|
2016-08-27 21:36:32 +03:00
|
|
|
private void HandlePushReregistration()
|
|
|
|
{
|
2016-08-21 02:19:10 +03:00
|
|
|
var pushNotification = Resolver.Resolve<IPushNotification>();
|
2016-08-27 21:36:32 +03:00
|
|
|
var settings = Resolver.Resolve<ISettings>();
|
|
|
|
|
|
|
|
// Reregister for push token based on certain conditions
|
2016-08-21 02:19:10 +03:00
|
|
|
// ref https://github.com/rdelrosario/xamarin-plugins/issues/65
|
2016-08-27 21:36:32 +03:00
|
|
|
|
|
|
|
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)
|
2016-08-21 02:19:10 +03:00
|
|
|
{
|
2016-08-27 09:59:34 +03:00
|
|
|
pushNotification.Unregister();
|
2016-08-27 21:36:32 +03:00
|
|
|
if(Resolver.Resolve<IAuthService>().IsAuthenticated)
|
|
|
|
{
|
|
|
|
pushNotification.Register();
|
|
|
|
}
|
2016-08-21 02:19:10 +03:00
|
|
|
}
|
2016-06-18 09:45:46 +03:00
|
|
|
}
|
|
|
|
|
2016-08-27 21:36:32 +03:00
|
|
|
private bool InDebugMode()
|
|
|
|
{
|
|
|
|
#if DEBUG
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-06-18 09:45:46 +03:00
|
|
|
public override void OnTerminate()
|
|
|
|
{
|
|
|
|
base.OnTerminate();
|
|
|
|
UnregisterActivityLifecycleCallbacks(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnActivityCreated(Activity activity, Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
CrossCurrentActivity.Current.Activity = activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnActivityDestroyed(Activity activity)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnActivityPaused(Activity activity)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnActivityResumed(Activity activity)
|
|
|
|
{
|
|
|
|
CrossCurrentActivity.Current.Activity = activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnActivitySaveInstanceState(Activity activity, Bundle outState)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnActivityStarted(Activity activity)
|
|
|
|
{
|
|
|
|
CrossCurrentActivity.Current.Activity = activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnActivityStopped(Activity activity)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void StartPushService()
|
|
|
|
{
|
|
|
|
AppContext.StartService(new Intent(AppContext, typeof(PushNotificationService)));
|
2016-08-21 02:19:10 +03:00
|
|
|
if(Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
|
2016-06-18 09:45:46 +03:00
|
|
|
{
|
2016-08-21 02:19:10 +03:00
|
|
|
PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext,
|
|
|
|
typeof(PushNotificationService)), 0);
|
2016-06-18 09:45:46 +03:00
|
|
|
AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(AlarmService);
|
|
|
|
alarm.Cancel(pintent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void StopPushService()
|
|
|
|
{
|
|
|
|
AppContext.StopService(new Intent(AppContext, typeof(PushNotificationService)));
|
2016-08-21 02:19:10 +03:00
|
|
|
if(Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
|
2016-06-18 09:45:46 +03:00
|
|
|
{
|
2016-08-21 02:19:10 +03:00
|
|
|
PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext,
|
|
|
|
typeof(PushNotificationService)), 0);
|
2016-06-18 09:45:46 +03:00
|
|
|
AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(AlarmService);
|
|
|
|
alarm.Cancel(pintent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 07:13:28 +03:00
|
|
|
public static void SetIoc(Application application)
|
2016-06-18 09:45:46 +03:00
|
|
|
{
|
2017-01-28 07:13:28 +03:00
|
|
|
UserDialogs.Init(application);
|
2016-08-13 07:30:41 +03:00
|
|
|
|
2016-06-18 09:45:46 +03:00
|
|
|
var container = new UnityContainer();
|
|
|
|
|
|
|
|
container
|
2016-08-04 07:06:09 +03:00
|
|
|
// Android Stuff
|
2017-01-28 07:13:28 +03:00
|
|
|
.RegisterInstance(application.ApplicationContext)
|
|
|
|
.RegisterInstance<Application>(application)
|
2016-06-18 09:45:46 +03:00
|
|
|
// Services
|
|
|
|
.RegisterType<IDatabaseService, DatabaseService>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<ISqlService, SqlService>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<ISecureStorageService, KeyStoreStorageService>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<ICryptoService, CryptoService>(new ContainerControlledLifetimeManager())
|
2016-08-02 03:23:46 +03:00
|
|
|
.RegisterType<IKeyDerivationService, BouncyCastleKeyDerivationService>(new ContainerControlledLifetimeManager())
|
2016-06-18 09:45:46 +03:00
|
|
|
.RegisterType<IAuthService, AuthService>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<IFolderService, FolderService>(new ContainerControlledLifetimeManager())
|
2017-01-03 08:17:15 +03:00
|
|
|
.RegisterType<ILoginService, LoginService>(new ContainerControlledLifetimeManager())
|
2016-06-18 09:45:46 +03:00
|
|
|
.RegisterType<ISyncService, SyncService>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<IClipboardService, ClipboardService>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<IPushNotificationListener, PushNotificationListener>(new ContainerControlledLifetimeManager())
|
2016-06-22 05:29:29 +03:00
|
|
|
.RegisterType<IAppIdService, AppIdService>(new ContainerControlledLifetimeManager())
|
2016-07-02 09:01:47 +03:00
|
|
|
.RegisterType<IPasswordGenerationService, PasswordGenerationService>(new ContainerControlledLifetimeManager())
|
2016-07-03 09:57:09 +03:00
|
|
|
.RegisterType<IReflectionService, ReflectionService>(new ContainerControlledLifetimeManager())
|
2016-07-20 06:29:32 +03:00
|
|
|
.RegisterType<ILockService, LockService>(new ContainerControlledLifetimeManager())
|
2016-07-24 06:50:08 +03:00
|
|
|
.RegisterType<IAppInfoService, AppInfoService>(new ContainerControlledLifetimeManager())
|
2016-08-04 07:06:09 +03:00
|
|
|
.RegisterType<IGoogleAnalyticsService, GoogleAnalyticsService>(new ContainerControlledLifetimeManager())
|
2016-08-24 05:43:17 +03:00
|
|
|
.RegisterType<IDeviceInfoService, DeviceInfoService>(new ContainerControlledLifetimeManager())
|
2016-11-26 18:51:04 +03:00
|
|
|
.RegisterType<ILocalizeService, LocalizeService>(new ContainerControlledLifetimeManager())
|
2016-12-23 08:39:00 +03:00
|
|
|
.RegisterType<ILogService, LogService>(new ContainerControlledLifetimeManager())
|
2016-12-24 18:54:18 +03:00
|
|
|
.RegisterType<IHttpService, HttpService>(new ContainerControlledLifetimeManager())
|
2017-02-04 09:12:25 +03:00
|
|
|
.RegisterType<ITokenService, TokenService>(new ContainerControlledLifetimeManager())
|
2017-02-09 04:44:35 +03:00
|
|
|
.RegisterType<ISettingsService, SettingsService>(new ContainerControlledLifetimeManager())
|
2017-02-15 03:47:00 +03:00
|
|
|
.RegisterType<IMemoryService, MemoryService>(new ContainerControlledLifetimeManager())
|
2016-06-18 09:45:46 +03:00
|
|
|
// Repositories
|
|
|
|
.RegisterType<IFolderRepository, FolderRepository>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<IFolderApiRepository, FolderApiRepository>(new ContainerControlledLifetimeManager())
|
2017-01-03 08:17:15 +03:00
|
|
|
.RegisterType<ILoginRepository, LoginRepository>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<ILoginApiRepository, LoginApiRepository>(new ContainerControlledLifetimeManager())
|
2017-02-04 09:12:25 +03:00
|
|
|
.RegisterType<IConnectApiRepository, ConnectApiRepository>(new ContainerControlledLifetimeManager())
|
2016-06-18 23:10:09 +03:00
|
|
|
.RegisterType<IDeviceApiRepository, DeviceApiRepository>(new ContainerControlledLifetimeManager())
|
2016-06-26 03:54:17 +03:00
|
|
|
.RegisterType<IAccountsApiRepository, AccountsApiRepository>(new ContainerControlledLifetimeManager())
|
2016-06-30 04:59:18 +03:00
|
|
|
.RegisterType<ICipherApiRepository, CipherApiRepository>(new ContainerControlledLifetimeManager())
|
2017-02-09 04:39:37 +03:00
|
|
|
.RegisterType<ISettingsRepository, SettingsRepository>(new ContainerControlledLifetimeManager())
|
2017-02-09 05:19:09 +03:00
|
|
|
.RegisterType<ISettingsApiRepository, SettingsApiRepository>(new ContainerControlledLifetimeManager())
|
2016-06-18 09:45:46 +03:00
|
|
|
// Other
|
|
|
|
.RegisterInstance(CrossSettings.Current, new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterInstance(CrossConnectivity.Current, new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterInstance(UserDialogs.Instance, new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterInstance(CrossFingerprint.Current, new ContainerControlledLifetimeManager());
|
|
|
|
|
2016-08-13 07:30:41 +03:00
|
|
|
CrossPushNotification.Initialize(container.Resolve<IPushNotificationListener>(), "962181367620");
|
2016-06-18 09:45:46 +03:00
|
|
|
container.RegisterInstance(CrossPushNotification.Current, new ContainerControlledLifetimeManager());
|
|
|
|
|
|
|
|
Resolver.SetResolver(new UnityResolver(container));
|
|
|
|
CrossFingerprint.SetCurrentActivityResolver(() => CrossCurrentActivity.Current.Activity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|