private void DisconnectFromService()
        {
            // remove service helper from UI
            _app.SensusMainPage.RemoveServiceHelper();

            // make service helper inaccessible to UI
            UiBoundSensusServiceHelper.Set(null);

            // unbind from service
            if (_serviceConnection.Binder != null)
            {
                // it's happened that the service is created 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 (_serviceConnection.Binder.SensusServiceHelper != null)
                {
                    _serviceConnection.Binder.SensusServiceHelper.SetMainActivity(null);
                    _serviceConnection.Binder.SensusServiceHelper.SaveAsync();
                }

                if (_serviceConnection.Binder.IsBound)
                {
                    UnbindService(_serviceConnection);
                }
            }
        }
Example #2
0
        public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
        {
            SensusServiceHelper.Initialize(() => new iOSSensusServiceHelper());

            // facebook settings
            Settings.AppID       = "873948892650954";
            Settings.DisplayName = "Sensus";

            Forms.Init();
            FormsMaps.Init();
            MapExtendRenderer.Init();

            ToastNotificatorImplementation.Init();

            App app = new App();

            LoadApplication(app);

            uiApplication.RegisterUserNotificationSettings(UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert, new NSSet()));

            _serviceHelper = SensusServiceHelper.Get() as iOSSensusServiceHelper;

            UiBoundSensusServiceHelper.Set(_serviceHelper);
            app.SensusMainPage.DisplayServiceHelper(UiBoundSensusServiceHelper.Get(true));

            return(base.FinishedLaunching(uiApplication, launchOptions));
        }
Example #3
0
        // This method should be used to release shared resources and it should store the application state.
        // If your application supports background exection this method is called instead of WillTerminate
        // when the user quits.
        public override void DidEnterBackground(UIApplication application)
        {
            iOSSensusServiceHelper serviceHelper = UiBoundSensusServiceHelper.Get(false) as iOSSensusServiceHelper;

            if (serviceHelper != null)
            {
                serviceHelper.SaveAsync();

                // app is no longer active, so reset the activation ID
                serviceHelper.ActivationId = null;
            }
        }
        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);
        }
Example #5
0
        public override void OnActivated(UIApplication uiApplication)
        {
            // since all notifications are about to be rescheduled, clear any scheduled / delivered notifications.
            UIApplication.SharedApplication.CancelAllLocalNotifications();
            UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;

            _serviceHelper.ActivationId = Guid.NewGuid().ToString();

            iOSSensusServiceHelper sensusServiceHelper = UiBoundSensusServiceHelper.Get(true) as iOSSensusServiceHelper;

            sensusServiceHelper.StartAsync(() =>
            {
                sensusServiceHelper.UpdateCallbackNotificationActivationIdsAsync();
            });

            base.OnActivated(uiApplication);
        }
Example #6
0
        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);
        }