protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SensusServiceHelper.Initialize(() => new AndroidSensusServiceHelper()); _uiReadyWait = new ManualResetEvent(false); _activityResultWait = new ManualResetEvent(false); _facebookCallbackManager = CallbackManagerFactory.Create(); _isForegrounded = false; Window.AddFlags(global::Android.Views.WindowManagerFlags.DismissKeyguard); Window.AddFlags(global::Android.Views.WindowManagerFlags.ShowWhenLocked); Window.AddFlags(global::Android.Views.WindowManagerFlags.TurnScreenOn); Forms.Init(this, savedInstanceState); FormsMaps.Init(this, savedInstanceState); MapExtendRenderer.Init(this, savedInstanceState); _app = new App(); LoadApplication(_app); _serviceConnection = new AndroidSensusServiceConnection(); _serviceConnection.ServiceConnected += (o, e) => { // it's happened that the service is created / started after the service helper is disposed: https://insights.xamarin.com/app/Sensus-Production/issues/46 // binding to the service in such a situation can result in a null service helper within the binder. if (e.Binder.SensusServiceHelper == null) { Finish(); return; } // get reference to service helper for use within the UI UiBoundSensusServiceHelper.Set(e.Binder.SensusServiceHelper); // give service helper a reference to this activity e.Binder.SensusServiceHelper.MainActivityWillBeSet = false; e.Binder.SensusServiceHelper.SetMainActivity(this); // display service helper properties on the main page _app.SensusMainPage.DisplayServiceHelper(e.Binder.SensusServiceHelper); }; _serviceConnection.ServiceDisconnected += (o, e) => { DisconnectFromService(); }; OpenIntentAsync(Intent); }
protected override void OnCreate(Bundle savedInstanceState) { Console.Error.WriteLine("--------------------------- Creating activity ---------------------------"); base.OnCreate(savedInstanceState); _activityResultWait = new ManualResetEvent(false); _facebookCallbackManager = CallbackManagerFactory.Create(); _serviceBindWait = new ManualResetEvent(false); Window.AddFlags(global::Android.Views.WindowManagerFlags.DismissKeyguard); Window.AddFlags(global::Android.Views.WindowManagerFlags.ShowWhenLocked); Window.AddFlags(global::Android.Views.WindowManagerFlags.TurnScreenOn); Forms.Init(this, savedInstanceState); FormsMaps.Init(this, savedInstanceState); ZXing.Net.Mobile.Forms.Android.Platform.Init(); CrossCurrentActivity.Current.Activity = this; #if UI_TESTING Forms.ViewInitialized += (sender, e) => { if (!string.IsNullOrWhiteSpace(e.View.StyleId)) { e.NativeView.ContentDescription = e.View.StyleId; } }; #endif LoadApplication(new App()); _serviceConnection = new AndroidSensusServiceConnection(); _serviceConnection.ServiceConnected += (o, e) => { // it's happened that the service is created / started after the service helper is disposed: https://insights.xamarin.com/app/Sensus-Production/issues/46 // binding to the service in such a situation can result in a null service helper within the binder. if the service helper was disposed, then the goal is // to close down sensus. so finish the activity. if (e.Binder.SensusServiceHelper == null) { Finish(); return; } // tell the service to finish this activity when it is stopped e.Binder.ServiceStopAction = Finish; // signal the activity that the service has been bound _serviceBindWait.Set(); // if we're UI testing, try to load and run the UI testing protocol from the embedded assets #if UI_TESTING using (Stream protocolFile = Assets.Open("UiTestingProtocol.json")) { Protocol.RunUiTestingProtocolAsync(protocolFile); } #endif }; // the following is fired if the process hosting the service crashes or is killed. _serviceConnection.ServiceDisconnected += (o, e) => { Toast.MakeText(this, "The Sensus service has crashed.", ToastLength.Long); DisconnectFromService(); Finish(); }; OpenIntentAsync(Intent); }
protected override async void OnCreate(Bundle savedInstanceState) { Console.Error.WriteLine("--------------------------- Creating activity ---------------------------"); // set the layout resources first ToolbarResource = Resource.Layout.Toolbar; TabLayoutResource = Resource.Layout.Tabbar; base.OnCreate(savedInstanceState); _activityResultWait = new ManualResetEvent(false); _serviceBindWait = new ManualResetEvent(false); Window.AddFlags(global::Android.Views.WindowManagerFlags.DismissKeyguard); Window.AddFlags(global::Android.Views.WindowManagerFlags.ShowWhenLocked); Window.AddFlags(global::Android.Views.WindowManagerFlags.TurnScreenOn); Forms.Init(this, savedInstanceState); FormsMaps.Init(this, savedInstanceState); ZXing.Net.Mobile.Forms.Android.Platform.Init(); // initialize the current activity plugin here as well as in the service, // since this activity will be starting the service after it is created. // only initializing the plugin in the service was keeping the plugin from // being properly initialized, presumably because the plugin missed the // lifecycle events of the activity. we want the plugin to have be // initialized regardless of how the app comes to be created. CrossCurrentActivity.Current.Init(this, savedInstanceState); #if UI_TESTING Forms.ViewInitialized += (sender, e) => { if (!string.IsNullOrWhiteSpace(e.View.StyleId)) { e.NativeView.ContentDescription = e.View.StyleId; } }; #endif LoadApplication(new App()); _serviceConnection = new AndroidSensusServiceConnection(); _serviceConnection.ServiceConnected += (o, e) => { // service was created/connected, so the service helper must exist. SensusServiceHelper.Get().Logger.Log("Bound to Android service.", LoggingLevel.Normal, GetType()); // tell the service to finish this activity when it is stopped e.Binder.OnServiceStop = Finish; // signal the activity that the service has been bound _serviceBindWait.Set(); // if we're UI testing, try to load and run the UI testing protocol from the embedded assets #if UI_TESTING using (Stream protocolFile = Assets.Open("UiTestingProtocol.json")) { Protocol.RunUiTestingProtocolAsync(protocolFile); } #endif }; // the following is fired if the process hosting the service crashes or is killed. _serviceConnection.ServiceDisconnected += (o, e) => { Toast.MakeText(this, "The Sensus service has crashed.", ToastLength.Long); DisconnectFromService(); Finish(); }; // ensure the service is started any time the activity is created AndroidSensusService.Start(false); await OpenIntentAsync(Intent); }
protected override void OnCreate(Bundle savedInstanceState) { Console.Error.WriteLine("--------------------------- Creating activity ---------------------------"); base.OnCreate(savedInstanceState); _activityResultWait = new ManualResetEvent(false); _facebookCallbackManager = CallbackManagerFactory.Create(); _serviceBindWait = new ManualResetEvent(false); Window.AddFlags(global::Android.Views.WindowManagerFlags.DismissKeyguard); Window.AddFlags(global::Android.Views.WindowManagerFlags.ShowWhenLocked); Window.AddFlags(global::Android.Views.WindowManagerFlags.TurnScreenOn); Forms.Init(this, savedInstanceState); FormsMaps.Init(this, savedInstanceState); MapExtendRenderer.Init(this, savedInstanceState); CrossCurrentActivity.Current.Activity = this; #if UNIT_TESTING Forms.ViewInitialized += (sender, e) => { if (!string.IsNullOrWhiteSpace(e.View.StyleId)) e.NativeView.ContentDescription = e.View.StyleId; }; #endif _app = new App(); LoadApplication(_app); _serviceConnection = new AndroidSensusServiceConnection(); _serviceConnection.ServiceConnected += (o, e) => { // it's happened that the service is created / started after the service helper is disposed: https://insights.xamarin.com/app/Sensus-Production/issues/46 // binding to the service in such a situation can result in a null service helper within the binder. if the service helper was disposed, then the goal is // to close down sensus. so finish the activity. if (e.Binder.SensusServiceHelper == null) { Finish(); return; } if (e.Binder.SensusServiceHelper.BarcodeScanner == null) { try { e.Binder.SensusServiceHelper.BarcodeScanner = new ZXing.Mobile.MobileBarcodeScanner(); } catch (Exception ex) { e.Binder.SensusServiceHelper.Logger.Log("Failed to create barcode scanner: " + ex.Message, LoggingLevel.Normal, GetType()); } } // tell the service to finish this activity when it is stopped e.Binder.ServiceStopAction = Finish; // signal the activity that the service has been bound _serviceBindWait.Set(); // if we're unit testing, try to load and run the unit testing protocol from the embedded assets #if UNIT_TESTING using (Stream protocolFile = Assets.Open("UnitTestingProtocol.json")) { Protocol.RunUnitTestingProtocol(protocolFile); } #endif }; // the following is fired if the process hosting the service crashes or is killed. _serviceConnection.ServiceDisconnected += (o, e) => { Toast.MakeText(this, "The Sensus service has crashed.", ToastLength.Long); DisconnectFromService(); Finish(); }; OpenIntentAsync(Intent); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); _uiReadyWait = new ManualResetEvent(false); _activityResultWait = new ManualResetEvent(false); Window.AddFlags(global::Android.Views.WindowManagerFlags.DismissKeyguard); Window.AddFlags(global::Android.Views.WindowManagerFlags.ShowWhenLocked); Window.AddFlags(global::Android.Views.WindowManagerFlags.TurnScreenOn); Forms.Init(this, savedInstanceState); App app = new App(); LoadApplication(app); // start service -- if it's already running, this will have no effect Intent serviceIntent = new Intent(this, typeof(AndroidSensusService)); StartService(serviceIntent); // bind UI to the service _serviceConnection = new AndroidSensusServiceConnection(); _serviceConnection.ServiceConnected += (o, e) => { // get reference to service helper for use within the UI UiBoundSensusServiceHelper.Set(e.Binder.SensusServiceHelper); // stop activity when service stops UiBoundSensusServiceHelper.Get(true).Stopped += (oo, ee) => { Finish(); }; // give service a reference to this activity (UiBoundSensusServiceHelper.Get(true) as AndroidSensusServiceHelper).SetMainActivity(this); // display service helper properties on the main page app.SensusMainPage.DisplayServiceHelper(UiBoundSensusServiceHelper.Get(true)); #region open page to view protocol if a protocol was passed to us if (Intent.Data != null) { global::Android.Net.Uri dataURI = Intent.Data; Action <Protocol> protocolDeserializedCallback = protocol => { if (protocol != null) { Device.BeginInvokeOnMainThread(async() => { try { UiBoundSensusServiceHelper.Get(true).RegisterProtocol(protocol); await app.MainPage.Navigation.PushAsync(new ProtocolsPage()); } catch (Exception ex) { string message = "Failed to register/display new protocol: " + ex.Message; SensusServiceHelper.Get().Logger.Log(message, LoggingLevel.Normal, GetType()); new AlertDialog.Builder(this).SetTitle("Failed to show protocol").SetMessage(message).Show(); } }); } }; try { if (Intent.Scheme == "http" || Intent.Scheme == "https") { Protocol.FromWebUriAsync(new Uri(dataURI.ToString()), protocolDeserializedCallback); } else if (Intent.Scheme == "content" || Intent.Scheme == "file") { Stream stream = null; try { stream = ContentResolver.OpenInputStream(dataURI); } catch (Exception ex) { SensusServiceHelper.Get().Logger.Log("Failed to open local protocol file URI \"" + dataURI + "\": " + ex.Message, LoggingLevel.Normal, GetType()); } if (stream != null) { Protocol.FromStreamAsync(stream, protocolDeserializedCallback); } } else { SensusServiceHelper.Get().Logger.Log("Sensus didn't know what to do with URI \"" + dataURI + "\".", LoggingLevel.Normal, GetType()); } } catch (Exception ex) { new AlertDialog.Builder(this).SetTitle("Failed to get protocol").SetMessage(ex.Message).Show(); } } #endregion }; _serviceConnection.ServiceDisconnected += (o, e) => { // do the opposite of what's in ServiceConnected UiBoundSensusServiceHelper.Set(null); e.Binder.SensusServiceHelper.SetMainActivity(null); }; BindService(serviceIntent, _serviceConnection, Bind.AutoCreate); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SensusServiceHelper.Initialize(() => new AndroidSensusServiceHelper()); _uiReadyWait = new ManualResetEvent(false); _activityResultWait = new ManualResetEvent(false); _facebookCallbackManager = CallbackManagerFactory.Create(); Window.AddFlags(global::Android.Views.WindowManagerFlags.DismissKeyguard); Window.AddFlags(global::Android.Views.WindowManagerFlags.ShowWhenLocked); Window.AddFlags(global::Android.Views.WindowManagerFlags.TurnScreenOn); Forms.Init(this, savedInstanceState); FormsMaps.Init(this, savedInstanceState); MapExtendRenderer.Init(this, savedInstanceState); _app = new App(); LoadApplication(_app); _serviceConnection = new AndroidSensusServiceConnection(); _serviceConnection.ServiceConnected += (o, e) => { // get reference to service helper for use within the UI UiBoundSensusServiceHelper.Set(e.Binder.SensusServiceHelper); // give service helper a reference to this activity e.Binder.SensusServiceHelper.MainActivityWillBeSet = false; e.Binder.SensusServiceHelper.SetMainActivity(this); // display service helper properties on the main page _app.SensusMainPage.DisplayServiceHelper(e.Binder.SensusServiceHelper); }; _serviceConnection.ServiceDisconnected += (o, e) => { DisconnectFromService(); }; OpenIntentAsync(Intent); }