Ejemplo n.º 1
0
        public static Task <bool> LaunchCortanaToConsentPageAsyncIfNeeded()
        {
            var isCortanaSupported = false;

            try
            {
                isCortanaSupported = CortanaSettings.IsSupported();
            }
            catch (UnauthorizedAccessException)
            {
                // This is indicitive of EmbeddedMode not being enabled (i.e.
                // running IotCoreDefaultApp on Desktop or Mobile without
                // enabling EmbeddedMode)
                //  https://developer.microsoft.com/en-us/windows/iot/docs/embeddedmode
            }

            // Do nothing for devices that do not support Cortana
            if (isCortanaSupported)
            {
                // Ordinarily, this is run during a first use Out-of-box-Experience (OOBE) and voice consent will NOT be granted.
                // So, we launch Cortana to it's Consent Page as part of OOBE to give the end-user an opportunity to give consent.
                // However on development systems where the IotCoreDefaultApp may be deployed repeatedly after Cortana consent has
                // already been granted we will bypass the Cortana Launch.
                var cortanaSettings = CortanaSettings.GetDefault();
                if (!cortanaSettings.HasUserConsentToVoiceActivation)
                {
                    return(LaunchCortanaToConsentPageAsync());
                }
            }

            return(Task.FromResult <bool>(false));
        }
Ejemplo n.º 2
0
        const int TIMEINTERVAL_VOICEACTIVATION = 10;    // milli sec
        private async Task SetVoiceActivation(bool value)
        {
            var cortanaSettings = CortanaSettings.GetDefault();

            for (int i = 0; i < MAX_VOICEACTIVATION_TRIALS; i++)
            {
                try
                {
                    cortanaSettings.IsVoiceActivationEnabled = value;
                }
                catch (System.Exception ex)
                {
                    if (ex.HResult == RPC_S_CALL_FAILED ||
                        ex.HResult == RPC_S_SERVER_UNAVAILABLE ||
                        ex.HResult == RPC_S_SERVER_TOO_BUSY)
                    {
                        // VoiceActivation server is very likely busy =>
                        // yield and take a new ref to CortanaSettings API
                        await Task.Delay(TimeSpan.FromMilliseconds(TIMEINTERVAL_VOICEACTIVATION));

                        cortanaSettings = CortanaSettings.GetDefault();
                    }
                    else
                    {
                        throw ex;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private void SetupCortana()
        {
            var isCortanaSupported = CortanaSettings.IsSupported();

            cortanaConsentRequestedFromSwitch = false;

            // Only allow the Cortana settings to be enabled if Cortana is available on this device
            CortanaVoiceActivationSwitch.IsEnabled = isCortanaSupported;
            CortanaAboutMeButton.IsEnabled         = isCortanaSupported;

            // If Cortana is supported on this device and the user has never granted voice consent,
            // then set a flag so that each time this page is activated we will poll for
            // Cortana's Global Consent Value and update the UI if needed.
            if (isCortanaSupported)
            {
                var cortanaSettings = CortanaSettings.GetDefault();
                needsCortanaConsent = !cortanaSettings.HasUserConsentToVoiceActivation;

                // If consent isn't needed, then update the voice activation switch to reflect its current system state.
                if (!needsCortanaConsent)
                {
                    CortanaVoiceActivationSwitch.IsOn = cortanaSettings.IsVoiceActivationEnabled;
                }
            }
        }
Ejemplo n.º 4
0
        async void CortanaVoiceActivation(bool active)
        {
            var cortanaSettings = CortanaSettings.GetDefault();
            var cortanaVoiceActivationSwitch = SwitchCotana;

            bool enableVoiceActivation = active;

            // If user is requesting to turn on voice activation, but consent has not been provided yet, then launch Cortana to ask for consent first
            if (!cortanaSettings.HasUserConsentToVoiceActivation)
            {
                // Guard against the case where the switch is toggled off when Consent hasn't been given yet
                // This occurs when we are re-entering this method when the switch is turned off in the code that follows
                if (!enableVoiceActivation)
                {
                    return;
                }

                cortanaVoiceActivationSwitch.IsEnabled = false;
                cortanaVoiceActivationSwitch.IsChecked = false;
            }
            // Otherwise, we already have consent, so just enable or disable the voice activation setting.
            // Do this asynchronously because the API waits for the SpeechRuntime EXE to launch
            else
            {
                cortanaVoiceActivationSwitch.IsEnabled = false;
                await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                {
                    await SetVoiceActivation(enableVoiceActivation);
                    cortanaVoiceActivationSwitch.IsEnabled = true;
                });
            }
        }
Ejemplo n.º 5
0
 public static void LaunchCortanaToConsentPageAsyncIfNeeded()
 {
     // Do nothing for devices that do not support Cortana
     if (CortanaSettings.IsSupported())
     {
         // Ordinarily, this is run during a first use Out-of-box-Experience (OOBE) and voice consent will NOT be granted.
         // So, we launch Cortana to it's Consent Page as part of OOBE to give the end-user an opportunity to give consent.
         // However on development systems where the IotCoreDefaultApp may be deployed repeatedly after Cortana consent has
         // already been granted we will bypass the Cortana Launch.
         var cortanaSettings = CortanaSettings.GetDefault();
         if (!cortanaSettings.HasUserConsentToVoiceActivation)
         {
             LaunchCortanaToConsentPageAsync();
         }
     }
 }
Ejemplo n.º 6
0
        public static bool IsCortanaSupported()
        {
            var isCortanaSupported = false;

            try
            {
                isCortanaSupported = CortanaSettings.IsSupported();
            }
            catch (UnauthorizedAccessException)
            {
                // This is indicitive of EmbeddedMode not being enabled (i.e.
                // running IotCoreDefaultApp on Desktop or Mobile without
                // enabling EmbeddedMode)
                //  https://developer.microsoft.com/en-us/windows/iot/docs/embeddedmode
            }

            return(isCortanaSupported);
        }
Ejemplo n.º 7
0
        private void Window_Activated(object sender, WindowActivatedEventArgs e)
        {
#if BUILDWITHCORTANA
            switch (e.WindowActivationState)
            {
            case CoreWindowActivationState.PointerActivated:
            case CoreWindowActivationState.CodeActivated:
                if (needsCortanaConsent)
                {
                    // Re-enable the voice activation selection
                    CortanaVoiceActivationSwitch.IsEnabled = true;

                    // Verify whether consent has changed while the screen was away
                    var cortanaSettings = CortanaSettings.GetDefault();
                    if (cortanaSettings.HasUserConsentToVoiceActivation)
                    {
                        // Consent was granted, so finish the task of flipping the switch to the current activation-state
                        // (It is possible that Cortana Consent was granted by some other application, while
                        // the default app was running, but not by the user actively flipping the switch,
                        // so update the switch state to the current global setting)
                        if (cortanaConsentRequestedFromSwitch)
                        {
                            cortanaSettings.IsVoiceActivationEnabled = true;
                            cortanaConsentRequestedFromSwitch        = false;
                        }

                        // Set the switch to the current global state
                        CortanaVoiceActivationSwitch.IsOn = cortanaSettings.IsVoiceActivationEnabled;

                        // We no longer need consent
                        needsCortanaConsent = false;
                    }
                }
                break;

            default:
                break;
            }
#endif
        }
Ejemplo n.º 8
0
        private void CortanaVoiceActivationSwitch_Toggled(object sender, RoutedEventArgs e)
        {
#if BUILDWITHCORTANA
            var cortanaSettings = CortanaSettings.GetDefault();
            var cortanaVoiceActivationSwitch = (ToggleSwitch)sender;

            bool enableVoiceActivation = cortanaVoiceActivationSwitch.IsOn;

            // If user is requesting to turn on voice activation, but consent has not been provided yet, then launch Cortana to ask for consent first
            if (!cortanaSettings.HasUserConsentToVoiceActivation)
            {
                // Guard against the case where the switch is toggled off when Consent hasn't been given yet
                // This occurs when we are re-entering this method when the switch is turned off in the code that follows
                if (!enableVoiceActivation)
                {
                    return;
                }

                // Launch Cortana to get the User Consent.  This is required before a change to enable voice activation is permitted
                CortanaVoiceActivationSwitch.IsEnabled = false;
                needsCortanaConsent = true;
                CortanaVoiceActivationSwitch.IsOn = false;
                cortanaConsentRequestedFromSwitch = true;
                CortanaHelper.LaunchCortanaToConsentPageAsync();
            }
            // Otherwise, we already have consent, so just enable or disable the voice activation setting.
            // Do this asynchronously because the API waits for the SpeechRuntime EXE to launch
            else
            {
                CortanaVoiceActivationSwitch.IsEnabled = false;
                Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    cortanaSettings.IsVoiceActivationEnabled = enableVoiceActivation;
                    CortanaVoiceActivationSwitch.IsEnabled   = true;
                });
            }
#endif
        }
Ejemplo n.º 9
0
        void CheckCortana()
        {
            var isCortanaSupported = CortanaHelper.IsCortanaSupported();

            var cortanaVoiceActivationSwitch = SwitchCotana;

            cortanaVoiceActivationSwitch.IsEnabled = isCortanaSupported;

            // If Cortana is supported on this device and the user has never granted voice consent,
            // then set a flag so that each time this page is activated we will poll for
            // Cortana's Global Consent Value and update the UI if needed.
            if (isCortanaSupported)
            {
                var  cortanaSettings     = CortanaSettings.GetDefault();
                bool needsCortanaConsent = !cortanaSettings.HasUserConsentToVoiceActivation;

                // If consent isn't needed, then update the voice activation switch to reflect its current system state.
                if (!needsCortanaConsent)
                {
                    cortanaVoiceActivationSwitch.IsChecked = cortanaSettings.IsVoiceActivationEnabled;
                }
            }
        }
Ejemplo n.º 10
0
        private void SetupCortana()
        {
            var isCortanaSupported = false;

            try
            {
                isCortanaSupported = CortanaSettings.IsSupported();
            }
            catch (UnauthorizedAccessException)
            {
                // This is indicitive of EmbeddedMode not being enabled (i.e.
                // running IotCoreDefaultApp on Desktop or Mobile without
                // enabling EmbeddedMode)
                //  https://developer.microsoft.com/en-us/windows/iot/docs/embeddedmode
            }
            cortanaConsentRequestedFromSwitch = false;

            // Only allow the Cortana settings to be enabled if Cortana is available on this device
            CortanaVoiceActivationSwitch.IsEnabled = isCortanaSupported;
            CortanaAboutMeButton.IsEnabled         = isCortanaSupported;

            // If Cortana is supported on this device and the user has never granted voice consent,
            // then set a flag so that each time this page is activated we will poll for
            // Cortana's Global Consent Value and update the UI if needed.
            if (isCortanaSupported)
            {
                var cortanaSettings = CortanaSettings.GetDefault();
                needsCortanaConsent = !cortanaSettings.HasUserConsentToVoiceActivation;

                // If consent isn't needed, then update the voice activation switch to reflect its current system state.
                if (!needsCortanaConsent)
                {
                    CortanaVoiceActivationSwitch.IsOn = cortanaSettings.IsVoiceActivationEnabled;
                }
            }
        }