Exemple #1
0
 public void SetService(AndroidSensusService service)
 {
     _service             = service;
     _connectivityManager = _service.GetSystemService(Context.ConnectivityService) as ConnectivityManager;
     _deviceId            = Settings.Secure.GetString(_service.ContentResolver, Settings.Secure.AndroidId);
     _textToSpeech        = new AndroidTextToSpeech(_service);
     _wakeLock            = (_service.GetSystemService(Context.PowerService) as PowerManager).NewWakeLock(WakeLockFlags.Partial, "SENSUS");
 }
Exemple #2
0
        public AndroidTextToSpeech(AndroidSensusService service)
        {
            _textToSpeech  = new TextToSpeech(service, this);
            _initWait      = new ManualResetEvent(false);
            _utteranceWait = new ManualResetEvent(false);
            _disposed      = false;

            _textToSpeech.SetOnUtteranceProgressListener(this);
        }
        public AndroidTextToSpeech(AndroidSensusService service)
        {
            _textToSpeech = new TextToSpeech(service, this);
            _initWait = new ManualResetEvent(false);
            _utteranceWait = new ManualResetEvent(false);
            _disposed = false;

            _textToSpeech.SetOnUtteranceProgressListener(this);
        }
 public override void OnReceive(global::Android.Content.Context context, Intent intent)
 {
     // this method is usually called on the UI thread and can crash the app if it throws an exception
     try
     {
         AndroidSensusService.Start(true);
     }
     catch (Exception ex)
     {
         SensusException.Report("Exception in boot-start broadcast receiver:  " + ex.Message, ex);
     }
 }
        public AndroidTextToSpeech(AndroidSensusService service)
        {
            // initialize wait handles before passing the current object as a listener
            // below. if the listener OnInit method is called before the wait handles are
            // initialized we could get an NRE:  https://insights.xamarin.com/app/Sensus-Production/issues/1099
            _initWait      = new ManualResetEvent(false);
            _utteranceWait = new ManualResetEvent(false);
            _disposed      = false;

            // initialize speech module
            _textToSpeech = new TextToSpeech(service, this);
            _textToSpeech.SetLanguage(Java.Util.Locale.Default);
            _textToSpeech.SetOnUtteranceProgressListener(this);
        }
Exemple #6
0
        public void SetService(AndroidSensusService service)
        {
            _service             = service;
            _connectivityManager = _service.GetSystemService(Context.ConnectivityService) as ConnectivityManager;
            _notificationManager = _service.GetSystemService(Context.NotificationService) as NotificationManager;
            _deviceId            = Settings.Secure.GetString(_service.ContentResolver, Settings.Secure.AndroidId);
            _textToSpeech        = new AndroidTextToSpeech(_service);
            _wakeLock            = (_service.GetSystemService(Context.PowerService) as PowerManager).NewWakeLock(WakeLockFlags.Partial, "SENSUS");

            // must initialize after _deviceId is set
            if (Insights.IsInitialized)
            {
                Insights.Identify(_deviceId, "Device ID", _deviceId);
            }
        }
        protected override async void OnResume()
        {
            Console.Error.WriteLine("--------------------------- Resuming activity ---------------------------");

            base.OnResume();

            // temporarily hide UI while we bind to service. allowing the user to click around before the service helper is initialized
            // may result in a crash.
            (Xamarin.Forms.Application.Current as App).MasterPage.IsVisible = false;
            (Xamarin.Forms.Application.Current as App).DetailPage.IsVisible = false;

            // ensure the service is bound any time the activity is resumed
            BindService(AndroidSensusService.GetServiceIntent(false), _serviceConnection, Bind.AboveClient);

            // start new task to wait for connection, since we're currently on the UI thread, which the service connection needs in order to complete.
            await Task.Run(() =>
            {
                // we've not seen the binding take more than a second or two; however, we want to be very careful not to block indefinitely
                // here because the UI is currently disabled. if for some strange reason the binding does not work, bail out after 10 seconds
                // and let the user interact with the UI. most likely, a crash will be coming very soon in this case, as the sensus service
                // will probably not be running. again, this has not occurred in practice, but allowing the crash to occur will send us information
                // through the crash analytics service and we'll be able to track it
                TimeSpan serviceBindTimeout = TimeSpan.FromSeconds(10000);
                if (_serviceBindWait.WaitOne(serviceBindTimeout))
                {
                    SensusServiceHelper.Get().Logger.Log("Activity proceeding following service bind.", LoggingLevel.Normal, GetType());
                }
                else
                {
                    SensusException.Report("Timed out waiting " + serviceBindTimeout + " for the service to bind.");
                }

                SensusServiceHelper.Get().CancelPendingSurveysNotification();

                // enable the UI
                try
                {
                    SensusContext.Current.MainThreadSynchronizer.ExecuteThreadSafe(() =>
                    {
                        (Xamarin.Forms.Application.Current as App).MasterPage.IsVisible = true;
                        (Xamarin.Forms.Application.Current as App).DetailPage.IsVisible = true;
                    });
                }
                catch (Exception)
                {
                }
            });
        }
        public void SetService(AndroidSensusService service)
        {
            _service = service;

            if (_service == null)
            {
                if (_wakeLock != null)
                {
                    _wakeLock.Dispose();
                    _wakeLock = null;
                }
            }
            else
            {
                _wakeLock = (_service.GetSystemService(global::Android.Content.Context.PowerService) as PowerManager).NewWakeLock(WakeLockFlags.Partial, "SENSUS");
                _wakeLockAcquisitionCount = 0;
                _deviceId = Settings.Secure.GetString(_service.ContentResolver, Settings.Secure.AndroidId);
            }
        }
        protected override void OnResume()
        {
            Console.Error.WriteLine("--------------------------- Resuming activity ---------------------------");

            base.OnResume();

            CrossCurrentActivity.Current.Activity = this;

            // temporarily disable UI while we bind to service
            Window.AddFlags(global::Android.Views.WindowManagerFlags.NotTouchable);

            // make sure that the service is running and bound any time the activity is resumed. the service is both started
            // and bound, as we'd like the service to remain running and available to other apps even if the current activity unbinds.
            Intent serviceIntent = AndroidSensusService.StartService(this, false);

            BindService(serviceIntent, _serviceConnection, Bind.AboveClient);

            // start new task to wait for connection, since we're currently on the UI thread, which the service connection needs in order to complete.
            Task.Run(() =>
            {
                // we've not seen the binding take more than a second or two; however, we want to be very careful not to block indefinitely
                // here becaus the UI is currently disabled. if for some strange reason the binding does not work, bail out after 10 seconds
                // and let the user interact with the UI. most likely, a crash will be coming very soon in this case, as the sensus service
                // will probably not be running. again, this has not occurred in practice, but allowing the crash to occur will send us information
                // through the crash analytics service and we'll be able to track it
                _serviceBindWait.WaitOne(TimeSpan.FromSeconds(10000));

                SensusServiceHelper.Get().ClearPendingSurveysNotificationAsync();

                // now that the service connection has been established, reenable UI.
                try
                {
                    SensusContext.Current.MainThreadSynchronizer.ExecuteThreadSafe(() =>
                    {
                        Window.ClearFlags(global::Android.Views.WindowManagerFlags.NotTouchable);
                    });
                }
                catch (Exception)
                {
                }
            });
        }
        public void SetService(AndroidSensusService service)
        {
            _service = service;

            if (_service == null)
            {
                if (_connectivityManager != null)
                {
                    _connectivityManager.Dispose();
                    _connectivityManager = null;
                }

                if (_notificationManager != null)
                {
                    _notificationManager.Dispose();
                    _notificationManager = null;
                }

                if (_textToSpeech != null)
                {
                    _textToSpeech.Dispose();
                    _textToSpeech = null;
                }

                if (_wakeLock != null)
                {
                    _wakeLock.Dispose();
                    _wakeLock = null;
                }
            }
            else
            {
                _connectivityManager      = _service.GetSystemService(Context.ConnectivityService) as ConnectivityManager;
                _notificationManager      = _service.GetSystemService(Context.NotificationService) as NotificationManager;
                _textToSpeech             = new AndroidTextToSpeech(_service);
                _wakeLock                 = (_service.GetSystemService(Context.PowerService) as PowerManager).NewWakeLock(WakeLockFlags.Partial, "SENSUS");
                _wakeLockAcquisitionCount = 0;
                _deviceId                 = Settings.Secure.GetString(_service.ContentResolver, Settings.Secure.AndroidId);
            }
        }
Exemple #11
0
        protected override void OnResume()
        {
            Console.Error.WriteLine("--------------------------- Resuming activity ---------------------------");

            base.OnResume();

            CrossCurrentActivity.Current.Activity = this;

            // make sure that the service is running and bound any time the activity is resumed. the service is both started
            // and bound, as we'd like the service to remain running and available to other apps even if the current activity unbinds.
            Intent serviceIntent = AndroidSensusService.StartService(this);

            BindService(serviceIntent, _serviceConnection, Bind.AboveClient);

            // prevent the user from interacting with the UI by displaying a progress dialog until
            // the service has been bound. if the service has already bound, the wait handle below
            // will already be set and the dialog will immediately be dismissed.
            ProgressDialog serviceBindWaitDialog = ProgressDialog.Show(this, "Please Wait", "Binding to Sensus", true, false);

            // start new thread to wait for connection, since we're currently on the UI thread, which the service connection needs in order to complete.
            Task.Run(() =>
            {
                _serviceBindWait.WaitOne();

                SensusServiceHelper.Get().ClearPendingSurveysNotificationAsync();

                // now that the service connection has been established, dismiss the wait dialog. wrap this in a try-catch in
                // case the screen orientation changes before we get here, which will disconnect the dialog from the window manager
                // and will throw an exception.
                try
                {
                    SensusContext.Current.MainThreadSynchronizer.ExecuteThreadSafe(serviceBindWaitDialog.Dismiss);
                }
                catch (Exception)
                {
                }
            });
        }
        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);
        }
        public void SetService(AndroidSensusService service)
        {
            _service = service;

            if (_service == null)
            {
                if (_connectivityManager != null)
                    _connectivityManager.Dispose();

                if (_notificationManager != null)
                    _notificationManager.Dispose();

                if (_textToSpeech != null)
                    _textToSpeech.Dispose();

                if (_wakeLock != null)
                    _wakeLock.Dispose();
            }
            else
            {
                _connectivityManager = _service.GetSystemService(Context.ConnectivityService) as ConnectivityManager;
                _notificationManager = _service.GetSystemService(Context.NotificationService) as NotificationManager;
                _textToSpeech = new AndroidTextToSpeech(_service);
                _wakeLock = (_service.GetSystemService(Context.PowerService) as PowerManager).NewWakeLock(WakeLockFlags.Partial, "SENSUS");
                _wakeLockAcquisitionCount = 0;
                _deviceId = Settings.Secure.GetString(_service.ContentResolver, Settings.Secure.AndroidId);

                // must initialize after _deviceId is set
                if (Insights.IsInitialized)
                    Insights.Identify(_deviceId, "Device ID", _deviceId);
            }
        }
 public override void OnReceive(global::Android.Content.Context context, Intent intent)
 {
     AndroidSensusService.StartService(context, true);
 }
 public void SetService(AndroidSensusService service)
 {
     _service = service;
     _connectivityManager = _service.GetSystemService(Context.ConnectivityService) as ConnectivityManager;
     _notificationManager = _service.GetSystemService(Context.NotificationService) as NotificationManager;
     _deviceId = Settings.Secure.GetString(_service.ContentResolver, Settings.Secure.AndroidId);
     _textToSpeech = new AndroidTextToSpeech(_service);
     _wakeLock = (_service.GetSystemService(Context.PowerService) as PowerManager).NewWakeLock(WakeLockFlags.Partial, "SENSUS");
 }