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.DeviceInfo; using Plugin.Fingerprint; using Plugin.Settings; using PushNotification.Plugin; using PushNotification.Plugin.Abstractions; using XLabs.Ioc; using XLabs.Ioc.Unity; namespace Bit.Android { [Application] public class MainApplication : Application, Application.IActivityLifecycleCallbacks { public static Context AppContext; public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer) { if(!Resolver.IsSet) { SetIoc(); } } public override void OnCreate() { base.OnCreate(); RegisterActivityLifecycleCallbacks(this); AppContext = ApplicationContext; StartPushService(); var pushNotification = Resolver.Resolve(); #if DEBUG // When running in debug mode you must unregister the previous instance first or else things wont work // ref https://github.com/rdelrosario/xamarin-plugins/issues/65 pushNotification.Unregister(); #endif if(Resolver.Resolve().IsAuthenticated) { pushNotification.Register(); } } 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))); if(Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat) { PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(PushNotificationService)), 0); AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(AlarmService); alarm.Cancel(pintent); } } public static void StopPushService() { AppContext.StopService(new Intent(AppContext, typeof(PushNotificationService))); if(Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat) { PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(PushNotificationService)), 0); AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(AlarmService); alarm.Cancel(pintent); } } private void SetIoc() { UserDialogs.Init(this); var container = new UnityContainer(); container // Android Stuff .RegisterInstance(ApplicationContext) .RegisterInstance(this) // Services .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) // Repositories .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) // Other .RegisterInstance(CrossDeviceInfo.Current, new ContainerControlledLifetimeManager()) .RegisterInstance(CrossSettings.Current, new ContainerControlledLifetimeManager()) .RegisterInstance(CrossConnectivity.Current, new ContainerControlledLifetimeManager()) .RegisterInstance(UserDialogs.Instance, new ContainerControlledLifetimeManager()) .RegisterInstance(CrossFingerprint.Current, new ContainerControlledLifetimeManager()); CrossPushNotification.Initialize(container.Resolve(), "962181367620"); container.RegisterInstance(CrossPushNotification.Current, new ContainerControlledLifetimeManager()); Resolver.SetResolver(new UnityResolver(container)); CrossFingerprint.SetCurrentActivityResolver(() => CrossCurrentActivity.Current.Activity); } } }