Example #1
0
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            Debug.WriteLine("BackgroundDeviceWatcher.Run called");

            uint eventCount = 0;
            ApplicationDataContainer    settings       = ApplicationData.Current.LocalSettings;
            DeviceWatcherTriggerDetails triggerDetails = (DeviceWatcherTriggerDetails)taskInstance.TriggerDetails;

            Debug.WriteLine(String.Format("Trigger contains {0} events", triggerDetails.DeviceWatcherEvents.Count));

            foreach (DeviceWatcherEvent e in triggerDetails.DeviceWatcherEvents)
            {
                switch (e.Kind)
                {
                case DeviceWatcherEventKind.Add:
                    Debug.WriteLine("Add: " + e.DeviceInformation.Id);
                    break;

                case DeviceWatcherEventKind.Update:
                    Debug.WriteLine("Update: " + e.DeviceInformationUpdate.Id);
                    break;

                case DeviceWatcherEventKind.Remove:
                    Debug.WriteLine("Remove: " + e.DeviceInformationUpdate.Id);
                    break;
                }
            }
            ;

            if (null != settings.Values["eventCount"] &&
                settings.Values["eventCount"].GetType() == typeof(uint))
            {
                eventCount = (uint)settings.Values["eventCount"];
            }

            // Add the number of events for this trigger to the number of events received in the past
            eventCount += (uint)triggerDetails.DeviceWatcherEvents.Count;

            Debug.WriteLine(eventCount + " events processed for lifetime of trigger");

            settings.Values["eventCount"] = eventCount;
        }
Example #2
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            deferral = taskInstance.GetDeferral();

            StorageFolder folder = ApplicationData.Current.LocalFolder;

            //StorageFile logsFile = await folder.CreateFileAsync("BGtask.txt", CreationCollisionOption.OpenIfExists);
            //string txt = "[RUN]: " + DateTime.Now.ToString() + Environment.NewLine;
            //await FileIO.WriteTextAsync(logsFile, txt);

            // This event is signaled when the operation completes
            opCompletedEvent = new ManualResetEvent(false);
            SecondaryAuthenticationFactorAuthentication.AuthenticationStageChanged += OnStageChanged;
            //ShowToastNotification("BG Task Hit!");


            if (taskInstance.TriggerDetails is DeviceWatcherTriggerDetails)
            {
                DeviceWatcherTriggerDetails triggerDetails = (DeviceWatcherTriggerDetails)taskInstance.TriggerDetails;

                foreach (DeviceWatcherEvent e in triggerDetails.DeviceWatcherEvents)
                {
                    switch (e.Kind)
                    {
                    case DeviceWatcherEventKind.Add:
                        Debug.WriteLine("[RUN] Add: " + e.DeviceInformation.Id);
                        deferral = taskInstance.GetDeferral();
                        try
                        {
                            SecondaryAuthenticationFactorAuthenticationStageInfo authStageInfo = await SecondaryAuthenticationFactorAuthentication.GetAuthenticationStageInfoAsync();

                            SecondaryAuthenticationFactorAuthenticationStage stage = authStageInfo.Stage;
                            //Debugger.Break();
                            if ((authStageInfo.Stage == SecondaryAuthenticationFactorAuthenticationStage.WaitingForUserConfirmation) ||
                                (authStageInfo.Stage == SecondaryAuthenticationFactorAuthenticationStage.CollectingCredential))
                            {
                                System.Diagnostics.Debug.WriteLine("[RUN] Perform Auth / plug trigger");
                                Task  t = PerformAuthentication();
                                await t;
                            }
                            //Debugger.Break();
                            if (e.DeviceInformation.Name.Contains("Ledger"))
                            {
                                Task  t = writeConnectedRegisteredDevices();
                                await t;
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                        finally
                        {
                            deferral.Complete();
                        }
                        break;

                    //case DeviceWatcherEventKind.Update:
                    //    Debug.WriteLine("[RUN] Update: " + e.DeviceInformationUpdate.Id);
                    //    deferral = taskInstance.GetDeferral();
                    //
                    //    try
                    //    {
                    //        Task i = writeConnectedRegisteredDevices();
                    //        await i;
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        Debugger.Break();
                    //    }
                    //    finally
                    //    {
                    //        deferral.Complete();
                    //    }
                    //    break;

                    case DeviceWatcherEventKind.Remove:
                        Debug.WriteLine("[RUN] Remove: " + e.DeviceInformationUpdate.Id);
                        deferral = taskInstance.GetDeferral();
                        try
                        {
                            List <dLock> pluggedRegisteredDeviceListBeforeRemove = await readConnectedRegisteredDevices();

                            List <Tuple <string, bool, bool> > pluggedRegisteredDeviceListBeforeRemove_tuple = new List <Tuple <string, bool, bool> >();
                            foreach (dLock device in pluggedRegisteredDeviceListBeforeRemove)
                            {
                                Tuple <string, bool, bool> newElem = new Tuple <string, bool, bool>(device.DeviceId, device.isDlockEnabled, device.isUsedForLastLogin);
                                pluggedRegisteredDeviceListBeforeRemove_tuple.Add(newElem);
                            }

                            IReadOnlyList <SecondaryAuthenticationFactorInfo> registeredDeviceList_removeEvent = await SecondaryAuthenticationFactorRegistration.FindAllRegisteredDeviceInfoAsync(
                                SecondaryAuthenticationFactorDeviceFindScope.User);

                            List <SecondaryAuthenticationFactorInfo> pluggedRegisteredDeviceListAfterRemove = await getConnectedRegisteredDeviceList(registeredDeviceList_removeEvent);

                            foreach (SecondaryAuthenticationFactorInfo deviceToCheck in pluggedRegisteredDeviceListAfterRemove)
                            {
                                foreach (dLock device in pluggedRegisteredDeviceListBeforeRemove)
                                {
                                    if (deviceToCheck.DeviceId == device.DeviceId)
                                    {
                                        var t = pluggedRegisteredDeviceListBeforeRemove_tuple.FirstOrDefault(i => i.Item1 == device.DeviceId);
                                        if (t != null)
                                        {
                                            // delete
                                            pluggedRegisteredDeviceListBeforeRemove_tuple.Remove(t);
                                        }
                                    }
                                }
                            }

                            bool   lockDevice = true;
                            string deviceId   = "";
                            for (int i = 0; i < pluggedRegisteredDeviceListBeforeRemove_tuple.Count(); i++)
                            {
                                deviceId = pluggedRegisteredDeviceListBeforeRemove_tuple[i].Item1;
                                if ((!pluggedRegisteredDeviceListBeforeRemove_tuple[i].Item2) || (!pluggedRegisteredDeviceListBeforeRemove_tuple[i].Item3))
                                {
                                    lockDevice = false;
                                }
                            }
                            if (lockDevice)
                            {
                                Task  t = LockDevice(deviceId);
                                await t;
                            }
                            await writeConnectedRegisteredDevices();
                        }
                        catch (Exception ex)
                        {
                        }
                        finally
                        {
                            deferral.Complete();
                        }
                        break;
                    }
                }
            }
            else
            {
                Debug.WriteLine("[RUN] Unknown trigger");
                deferral = taskInstance.GetDeferral();
                //try
                //{
                //    Task t = writeConnectedRegisteredDevices();
                //    await t;
                //}
                //catch (ConnectedRegisteredDevicesListTxtFileEmpty Ex)
                //{

                //}
                //finally
                //{
                //    deferral.Complete();
                //}
            }
            // Wait until the operation completes
            opCompletedEvent.WaitOne();

            deferral.Complete();
        }
Example #3
0
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            ApplicationDataContainer    settings       = ApplicationData.Current.LocalSettings;
            DeviceWatcherTriggerDetails triggerDetails = (DeviceWatcherTriggerDetails)taskInstance.TriggerDetails;

            bool   surfaceKeyboardAttached = false;
            int    numKeyboardsDetected    = 0;
            string lastID = "";

            foreach (DeviceWatcherEvent e in triggerDetails.DeviceWatcherEvents)
            {
                switch (e.Kind)
                {
                case DeviceWatcherEventKind.Add:
                    //Debug.WriteLine("Add: " + e.DeviceInformation.Id);
                    if ("Surface Keyboard".Equals(e.DeviceInformation.Name))
                    {
                        //Surface Keyboard being attached
                        //Reading sensor data to determine displays
                        Windows.Devices.Sensors.Inclinometer _inclinometer = Inclinometer.GetDefault();
                        if (_inclinometer != null)
                        {
                            Debug.WriteLine("Sensorwert: " + _inclinometer.GetCurrentReading().PitchDegrees);
                            settings.Values["pitch"] = _inclinometer.GetCurrentReading().PitchDegrees;
                            if (_inclinometer.GetCurrentReading().PitchDegrees < 20)
                            {
                                settings.Values["surfaceMode"] = "tablet";
                                sendMessage();
                            }
                            else
                            {
                                settings.Values["surfaceMode"] = "desktop";
                                sendMessage();
                            }
                        }
                        numKeyboardsDetected++;
                        lastID = e.DeviceInformation.Id;
                        //UPDATE. [System.Devices.InterfaceEnabled, True]
                        //surfaceKeyboardAttached = true;
                    }
                    break;

                case DeviceWatcherEventKind.Update:
                    //Debug.WriteLine("Update: " + e.DeviceInformationUpdate.Id);
                    if (settings.Values["id"] != null)
                    {
                        if (settings.Values["id"].Equals(e.DeviceInformationUpdate.Id))
                        {
                            Debug.WriteLine("Surface keyboard detached or attached");
                            surfaceKeyboardAttached = true;
                        }
                    }

                    //Debug.WriteLine("UPDATE. " + string.Join(",", e.DeviceInformationUpdate.Properties.ToList()));
                    break;

                case DeviceWatcherEventKind.Remove:
                    Debug.WriteLine("Remove: " + e.DeviceInformationUpdate.Id);
                    break;
                }
            }
            ;
            if (numKeyboardsDetected == 1)
            {
                settings.Values["id"] = lastID;
            }
            settings.Values["keyboardStatus"] = surfaceKeyboardAttached;
        }