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 Plugin.Connectivity;
|
|
|
|
using Plugin.CurrentActivity;
|
|
|
|
using Plugin.Fingerprint;
|
|
|
|
using Plugin.Settings;
|
|
|
|
using PushNotification.Plugin;
|
|
|
|
using PushNotification.Plugin.Abstractions;
|
|
|
|
using XLabs.Ioc;
|
2016-08-27 21:36:32 +03:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Plugin.Settings.Abstractions;
|
2017-02-16 05:56:02 +03:00
|
|
|
using FFImageLoading.Forms.Droid;
|
2017-06-01 21:49:48 +03:00
|
|
|
using XLabs.Ioc.SimpleInjectorContainer;
|
|
|
|
using SimpleInjector;
|
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)
|
|
|
|
{
|
2017-06-09 04:20:56 +03:00
|
|
|
// AndroidEnvironment.UnhandledExceptionRaiser += AndroidEnvironment_UnhandledExceptionRaiser;
|
2017-06-08 19:44:16 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 19:44:16 +03:00
|
|
|
private void AndroidEnvironment_UnhandledExceptionRaiser(object sender, RaiseThrowableEventArgs e)
|
|
|
|
{
|
|
|
|
var message = AppendExceptionToMessage("", e.Exception);
|
|
|
|
Utilities.SendCrashEmail(message, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private string AppendExceptionToMessage(string message, Exception ex)
|
|
|
|
{
|
|
|
|
message += ("\n\n" + ex.Message + "\n\n" + ex.StackTrace);
|
|
|
|
if(ex.InnerException != null)
|
|
|
|
{
|
|
|
|
return AppendExceptionToMessage(message, ex.InnerException);
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
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-17 05:14:02 +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);
|
2017-02-16 05:56:02 +03:00
|
|
|
CachedImageRenderer.Init();
|
2016-06-18 09:45:46 +03:00
|
|
|
CrossFingerprint.SetCurrentActivityResolver(() => CrossCurrentActivity.Current.Activity);
|
2017-06-01 21:49:48 +03:00
|
|
|
|
|
|
|
//var container = new UnityContainer();
|
|
|
|
var container = new Container();
|
|
|
|
|
|
|
|
// Android Stuff
|
|
|
|
container.RegisterSingleton(application.ApplicationContext);
|
|
|
|
container.RegisterSingleton<Application>(application);
|
|
|
|
|
|
|
|
// Services
|
|
|
|
container.RegisterSingleton<IDatabaseService, DatabaseService>();
|
|
|
|
container.RegisterSingleton<ISqlService, SqlService>();
|
2017-06-11 05:18:34 +03:00
|
|
|
container.RegisterSingleton<ISecureStorageService, AndroidKeyStoreStorageService>();
|
2017-06-01 21:49:48 +03:00
|
|
|
container.RegisterSingleton<ICryptoService, CryptoService>();
|
|
|
|
container.RegisterSingleton<IKeyDerivationService, BouncyCastleKeyDerivationService>();
|
|
|
|
container.RegisterSingleton<IAuthService, AuthService>();
|
|
|
|
container.RegisterSingleton<IFolderService, FolderService>();
|
|
|
|
container.RegisterSingleton<ILoginService, LoginService>();
|
|
|
|
container.RegisterSingleton<ISyncService, SyncService>();
|
|
|
|
container.RegisterSingleton<IClipboardService, ClipboardService>();
|
|
|
|
container.RegisterSingleton<IAppIdService, AppIdService>();
|
|
|
|
container.RegisterSingleton<IPasswordGenerationService, PasswordGenerationService>();
|
|
|
|
container.RegisterSingleton<IReflectionService, ReflectionService>();
|
|
|
|
container.RegisterSingleton<ILockService, LockService>();
|
|
|
|
container.RegisterSingleton<IAppInfoService, AppInfoService>();
|
|
|
|
container.RegisterSingleton<IGoogleAnalyticsService, GoogleAnalyticsService>();
|
|
|
|
container.RegisterSingleton<IDeviceInfoService, DeviceInfoService>();
|
|
|
|
container.RegisterSingleton<ILocalizeService, LocalizeService>();
|
|
|
|
container.RegisterSingleton<ILogService, LogService>();
|
|
|
|
container.RegisterSingleton<IHttpService, HttpService>();
|
|
|
|
container.RegisterSingleton<ITokenService, TokenService>();
|
|
|
|
container.RegisterSingleton<ISettingsService, SettingsService>();
|
|
|
|
container.RegisterSingleton<IMemoryService, MemoryService>();
|
|
|
|
container.RegisterSingleton<IAppSettingsService, AppSettingsService>();
|
|
|
|
|
|
|
|
// Repositories
|
|
|
|
container.RegisterSingleton<IFolderRepository, FolderRepository>();
|
|
|
|
container.RegisterSingleton<IFolderApiRepository, FolderApiRepository>();
|
|
|
|
container.RegisterSingleton<ILoginRepository, LoginRepository>();
|
|
|
|
container.RegisterSingleton<ILoginApiRepository, LoginApiRepository>();
|
|
|
|
container.RegisterSingleton<IConnectApiRepository, ConnectApiRepository>();
|
|
|
|
container.RegisterSingleton<IDeviceApiRepository, DeviceApiRepository>();
|
|
|
|
container.RegisterSingleton<IAccountsApiRepository, AccountsApiRepository>();
|
|
|
|
container.RegisterSingleton<ICipherApiRepository, CipherApiRepository>();
|
|
|
|
container.RegisterSingleton<ISettingsRepository, SettingsRepository>();
|
|
|
|
container.RegisterSingleton<ISettingsApiRepository, SettingsApiRepository>();
|
|
|
|
|
|
|
|
// Other
|
|
|
|
container.RegisterSingleton(CrossSettings.Current);
|
|
|
|
container.RegisterSingleton(CrossConnectivity.Current);
|
|
|
|
container.RegisterSingleton(UserDialogs.Instance);
|
|
|
|
container.RegisterSingleton(CrossFingerprint.Current);
|
|
|
|
|
|
|
|
// Push
|
|
|
|
var pushListener = new PushNotificationListener();
|
|
|
|
container.RegisterSingleton<IPushNotificationListener>(pushListener);
|
|
|
|
CrossPushNotification.Initialize(pushListener, "962181367620");
|
|
|
|
container.RegisterSingleton(CrossPushNotification.Current);
|
|
|
|
|
|
|
|
container.Verify();
|
|
|
|
Resolver.SetResolver(new SimpleInjectorResolver(container));
|
2016-06-18 09:45:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|